gpt4 book ai didi

Java程序高低游戏

转载 作者:行者123 更新时间:2023-12-01 14:03:00 26 4
gpt4 key购买 nike

我用 jQuery 写了这个,但是循环不正确,我看不到我的错误?当用户输入-99时它并没有结束?也不计算最高和最低数字?

import java.util.Scanner;

public class LargestandSmallest
{

public static void main (String [] args)
{

//Create a Scanner object for the keyboard input
Scanner keyboard = new Scanner(System.in);

//Declare local variable
double number;


//Explain what the program does
System.out.println ("This program will ask you to enter in a series of");
System.out.println ("numbers, and then will display the lowest and");
System.out.println ("highest numbers, out of what you enter, until you input");
System.out.println ("the value indicated to end the program.");


//Have the user input a series of numbers and continue processing
//until the user enters -99
System.out.println ("Please enter a number: (or -99 to end the program).");
number = keyboard.nextDouble();

//Display the table headings
System.out.println ("Lowest\tHighest");
System.out.println ("------------------");

//Call the method to caluclate the highest and lowest numbers
calculateLowHigh(number);
}

//Module called calculateTemp
public static void calculateLowHigh(double number)
{

//Declare local calculation variables
double highestNumber = 0;
double lowestNumber = 0;

//Set the parameters for running the loop
while (number != -99)
{
for(number = 0; number < 5; number++)
{

//Calculate the lowest and highest numbers entered
if (number > highestNumber) {
highestNumber = number;
}

if (number < lowestNumber) {
lowestNumber = number;
}

}
}

//Display the results
System.out.println (lowestNumber + "\t\t" + highestNumber);


}

}

最佳答案

您的问题是,在 calculateLowHigh 中,您将 number 变量用于两种不同的用途。它是将用户输入的数字传递给此方法的参数;但您还使用它作为 for 循环内的循环索引。也许您应该出于一个或另一个目的使用不同的变量。更好的是,完全放弃 for 循环 - 它似乎没有做任何事情。

此外,while循环应该在main中完成,而不是在calculateHighLow中完成,否则用户将只能有机会输入一个数字一次。

关于Java程序高低游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19192755/

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