我需要在此问一个循环,询问玩家是否想再玩一次。如果用户说是,则重启游戏。如果他们说没有结束比赛。我一共遇到麻烦。请帮忙!
import java.util.Scanner;
public class HW4ChrisMuncher
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double a;
int b;
System.out.println("Enter a number");
double userNum = input.nextDouble();
a = userNum;
Scanner input2 = new Scanner(System.in);
System.out.println("Enter a number (no decimals!)");
int userNum2 = input2.nextInt();
b = userNum2;
double c = min(a, b);
double d = max(a, b);
double e = abs(a);
double f = pow(a, b);
System.out.println("The minimum value of " + a + " and " + b + " is " + c );
System.out.println("The maximum value of " + a + " and " + b + " is " + d );
System.out.println("The absolute value of "+ a+ " is " + e );
System.out.println(+ a + " to the power of " + b + " is " + f );
}
// Returns the minimum of two numbers
public static double min(double n1, int n2)
{
double min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
// Return the max between two numbers
public static double max(double n1, int n2)
{
double max;
if (n1 > n2)
max = n1;
else
max = n2;
return max;
}
//Returns the absolute value of the two numbers
public static double abs(double n1)
{
if (n1 < 0)
return -n1;
else
return n1;
}
public static double pow(double n1, int n2)
{
double f = 1;
for (int i =0; i< n2; i++)
{
f = f * n1;
}
return f;
}
}
答案 0 :(得分:0)
很简单!
只需抓住一个while循环并执行以下操作:
String s = "yes";
while(s.equals("yes") || s.equals("y")) {
//whatever you want repeated
System.out.println("Would you like to repeat?");
s = scan.nextLine();
}
想一想。如果这个人输入“是”,那么while循环仍然是真的,所以它将从头开始。否则,如果此人输入“yes”之外的任何内容,则while循环条件将为false,并且循环将终止。
我希望有所帮助。祝你好运:)