gpt4 book ai didi

java - 用户在for循环java中输入更大的数字,错误 "bad operand type"

转载 作者:行者123 更新时间:2023-12-02 03:44:39 26 4
gpt4 key购买 nike

我需要用 Java 创建一个程序:

-要求 10 个不同的数字。

-告诉最大的数字。

我做了什么:

import java.util.Scanner;

public class BiggerValue {
public static void main (String [] args) {
Scanner sc = new Scanner(System.in);
int bigger;
int[] n = new int[10];
System.out.print("Please enter " + n.length + " values:");
for(int i = 0; i < n.length; i++) {
n[i] = sc.nextInt();
System.out.println(n[i]);
if(n < n[i]) {
bigger = num[i];
}
}
System.out.println("The highest number is " + bigger);
}
}

我不知道我这样做是否正确。我在编译时遇到错误 < n[i])

Bad operand types for binary operator '<'
first type:int[] second type:int

最佳答案

n 是数组类型,这导致了错误。他们使用以下代码。

import java.util.Scanner;

public class BiggerValue {
public static void main (String [] args) {
Scanner sc = new Scanner(System.in);
int bigger = 0; //you need to initialise this
int[] n = new int[10];
System.out.print("Please enter " + n.length + " values:");
for(int i = 0; i < n.length; i++) {
n[i] = sc.nextInt();
System.out.println(n[i]);
if(bigger < n[i]) { //you should compare it with bigger, not n.
bigger = n[i]; //there is nothing called num[i], it is n[i]
}
}
System.out.println("The highest number is " + bigger);
}
}

关于java - 用户在for循环java中输入更大的数字,错误 "bad operand type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36414008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com