英文:
The Biggest of Five Numbers
问题
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int d = sc.nextInt();
int e = sc.nextInt();
if ((a >= b) && (a >= c) && (a >= d) && (a >= e)) {
System.out.println(a);
} else if ((b >= c) && (b >= d) && (b >= e)) {
System.out.println(b);
} else if ((c >= d) && (c >= e)) {
System.out.println(c);
} else if (d >= e) {
System.out.println(d);
} else {
System.out.println(e);
}
}
英文:
I have a problem with my homework. My program has to work for integer and floating-point numbers.
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int d=sc.nextInt();
int e=sc.nextInt();
if ((a >= b) && (a >= c) && (a >= d) && (a >= e)) { // a >= b,c,d,e
System.out.println (a);
} else if ((b >= c) && (b >= d) && (b >= e)) { // b >= c,d,e
System.out.println ( b);
} else if ((c >= d) && (c >= e)) { // c >= d,e
System.out.println ( c);
} else if (d >= e) { // d >= e
System.out.println ( d);
} else { // e > d
System.out.println (e);
}
}
What's wrong with the code?
答案1
得分: 1
如果它需要适用于 int
和 "浮点数" ,那么您应该对所有五个值使用 Scannet.nextDouble()
(它们应该是 double
)。也就是说,int
没有 任何 浮点数部分。类似这样,
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble(),
d = sc.nextDouble(), e = sc.nextDouble();
System.out.println(Math.max(Math.max(Math.max(Math.max(a, b), c), d), e));
英文:
If it has to work for int
and "floating-point" numbers, then you should use Scannet.nextDouble()
for all five values (and they should be double
). That is, int
has no floating point component. Something like,
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble(),
d = sc.nextDouble(), e = sc.nextDouble();
System.out.println(Math.max(Math.max(Math.max(Math.max(a, b), c), d), e));
答案2
得分: 0
你可以使用 Collections 类中的 List。不确定这是否符合您的任务要求。
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
List<Double> nums = new ArrayList<>();
for(int x = 0; x < 5; x++)
{
nums.add(sc.nextDouble());
}
System.out.println("输入的最大数是 " + Collections.max(nums));
}
英文:
You could use a List with the Collections class. Not sure if this falls within the parameters of your assignment though.
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
List<Double> nums = new ArrayList<>();
for(int x = 0; x < 5; x++)
{
nums.add(sc.nextDouble());
}
System.out.println("The biggest number entered is " + Collections.max(nums));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论