类名:digui 注释:使用Scanner类对象来输入,输入字符串用sc.nextline 输入整数用sc.nextInt,输入实数用sc.nextDouble
时间: 2024-05-27 07:12:11 浏览: 15
import java.util.Scanner;
public class digui {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String str = sc.nextLine();
System.out.println("请输入一个整数:");
int num1 = sc.nextInt();
System.out.println("请输入一个实数:");
double num2 = sc.nextDouble();
System.out.println("您输入的字符串是:" + str);
System.out.println("您输入的整数是:" + num1);
System.out.println("您输入的实数是:" + num2);
}
}
相关问题
java任意输入一个double型的小时,转换位时,分,秒。类名:Zhuanhuan,输入6.5,输出:6时30分0秒 注释:使用Scanner类对象来输入,输入字符串用sc.nextline 输入整数用sc.nextInt,输入实数用sc.nextDouble
import java.util.Scanner;
public class Zhuanhuan {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入小时数:");
double hours = sc.nextDouble();
int h = (int) hours; // 小时数取整
double d = hours - h; // 小时数的小数部分
int m = (int) (d * 60); // 分钟数
int s = (int) ((d * 3600) % 60); // 秒数
System.out.println(h + "时" + m + "分" + s + "秒");
}
}
java输入两个正整数m和n,求其最大公约数和最小公倍数。类名:Maxmin,输入7,21,输出:最大公约数7,最小公倍数21 注释:使用Scanner类对象来输入,输入字符串用sc.nextline 输入整数用sc.nextInt,输入实数用sc.nextDouble
```
import java.util.Scanner;
public class Maxmin {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入两个正整数:");
int m = sc.nextInt();
int n = sc.nextInt();
int gcd = getGcd(m, n);
int lcm = getLcm(m, n, gcd);
System.out.println("最大公约数:" + gcd);
System.out.println("最小公倍数:" + lcm);
}
// 求最大公约数
public static int getGcd(int m, int n) {
while (m % n != 0) {
int temp = m % n;
m = n;
n = temp;
}
return n;
}
// 求最小公倍数
public static int getLcm(int m, int n, int gcd) {
return m * n / gcd;
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)