package oddsorevens;``
import java.util.Scanner;
import java.util.Random;
public class OddsOrEvens
{
public static void main(String[] args)
{
Scanner name= new Scanner (System.in); // getting input from user
System.out.print("Hi! What's your name: ");
String user = name.nextLine(); // getting user name
System.out.println("Hello: "+user);
System.out.println("Let's play OddsOrEvens");
System.out.println("Choose Odd or Even");
String s ="oddOrEven";
Scanner str = new Scanner(System.in); // getting new string from user
String s1 = str.next(); //storing odd or even here
o:
if (s1.equals("even")||(s1.startsWith("e")))
{
System.out.println("You choose even");
System.out.println("Computer choose odd");
}
else if (s1.equals("odd")||(s1.startsWith("o")))
{
System.out.println("You choose odd");
System.out.println("Computer choose even");
}
else
{
System.out.println("Entered wrong keyword");
}
System.out.print("How many fingers you want to put out: ");
Scanner num = new Scanner(System.in);
int n=num.nextInt();
Random rand = new Random();
int computer = rand.nextInt(6);
System.out.println("Computer choose "+ computer +" fingers");
int sum;
sum=n+computer;
System.out.println(sum);
if(sum%2==0)
{
System.out.println(sum+" is even");
}
else
{
System.out.println(sum+" is odd");
}
int u = s1.length();
if(u/2==0)
{
System.out.println("You won :)");
}
else
{``
System.out.println("You lose :(");
}
}
}
这是我在java中的第一个pgm。这里如果用户输入错误的关键字pgm应该是break并且根据用户输入pgm的输出将是win或lost。求助。
答案 0 :(得分:0)
每当您希望应用程序停止时,您必须明确告知您要退出。为此,您只需编写一段代码,即System.exit(0)
此处示例代码可以更好地理解它:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
if (s.next().equals("o")) {
System.out.println("game is still on");
} else {
System.out.println("game end!");
System.exit(0);
}
}