gpt4 book ai didi

java - 根据用户输入查找数组中的最大数字

转载 作者:行者123 更新时间:2023-12-02 04:07:25 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,该程序可以找到整数数组中比用户使用 JOptionPane 输入的最大数字。这是我到目前为止的代码,但在编译时出现错误。

import javaz.swing.JOptionPane;
public class Week9Largest {
public static void main(String[] args) {
int [] x = new int [7] ;
string myString;
int myInt;
for(int i = 0; i <= 6; i++){
myString = JOptionPane.showInputDialog (null, "Enter " + "integer " + (i + 1));
myInt = Integer.parseInt(myString);
x[i] = myInt;
}
int largest = Integer.MIN_VALUE;
for (int i=0;i<numbers.length;i++){
if(myInt[i]>largest){
largest = myInt[i];
}
}
System.out.println("Largest number in array is : " +largest);

}

最佳答案

更正这个:

 string myString;

致:

 String myString;

这是纠正一些问题后的代码,没问题:

 public static void main(String[] args) {
int[] x = new int[7];
String myString;
int myInt;
for (int i = 0; i <= 6; i++) {
myString = JOptionPane.showInputDialog(null, "Enter " + "integer "
+ (i + 1));
myInt = Integer.parseInt(myString);
x[i] = myInt;
}
int largest = Integer.MIN_VALUE;
for (int i = 0; i < x.length; i++) {
if (x[i] > largest) {
largest = x[i];
}
}
System.out.println("Largest number in array is : " + largest);

}

因为你在这个循环中有错误:

   for (int i=0;i<numbers.length;i++){
if(myInt[i]>largest){
largest = myInt[i];
}

修正后变成:

 for (int i = 0; i < x.length; i++) {
if (x[i] > largest) {
largest = x[i];
}

关于java - 根据用户输入查找数组中的最大数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34134156/

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