当用户输入一个数字(1 - 6)时,我无法使此程序显示消息“数量超出范围”。我尝试了不同的代码方式,但我不知道我做错了什么。
public static void main(String[] args) {
int guess = 0;
int tries = 3;
Scanner input = new Scanner(System.in);
Random ran = new Random();
int diceNumber = ran.nextInt(6)+1;
System.out.println(diceNumber);
System.out.println("Insert a number between 1 and 6 : you have " + tries + " tries.");
guess = input.nextInt();
while((tries >1 && (guess <7 || guess>0))) {
tries--;
System.out.println("Incorrect Number, you have " +tries+ " more tries.");
guess = input.nextInt();
}if(guess >=7 || guess<=0 && (tries >1)){
--tries;
System.out.println("Number out of range, try again; you have " + tries + " more tries.");
guess = input.nextInt();
} if(tries ==0 || guess != diceNumber) {
System.out.println("You Lose!!");
} else if(guess == diceNumber){
System.out.println("You Win!!");
}
}
答案 0 :(得分:0)
您需要定位条件以在while循环开始时检查输入是否是有效输入,当前,只有在结束之前的while循环所有尝试都用完后才会检查它。< / p>
此外,您应该将while循环条件从(tries >1 && (guess <7 || guess>0))
更改为(tries > 1 && guess != diceNumber)
,因为如果输入的数字超出范围,您可能不想退出循环。
代码 -
public static void main(String[] args) {
int guess = 0;
int tries = 3;
Scanner input = new Scanner(System.in);
Random ran = new Random();
int diceNumber = ran.nextInt(6)+1;
System.out.println(diceNumber);
System.out.println("Insert a number between 1 and 6 : you have " + tries + " tries.");
guess = input.nextInt();
while((tries >1 && guess != diceNumber)) {
if((guess >=7 || guess<=0) && (tries >1)){
--tries;
System.out.println("Number out of range, try again; you have " + tries + " more tries.");
guess = input.nextInt();
}
else {
tries--;
System.out.println("Incorrect Number, you have " +tries+ " more tries.");
guess = input.nextInt();
}
}
if(tries ==0 || guess != diceNumber) {
System.out.println("You Lose!!");
} else if(guess == diceNumber){
System.out.println("You Win!!");
}
}
答案 1 :(得分:0)
以下代码将获取预期结果。
Dealer.withCriteria {
vehicle {
eq 'liabilityInsurance', foo // error, vehicle doesn't have liabilityInsurance
}
}
答案 2 :(得分:0)
我认为问题出在你的<iframe id="chat" onLoad="updateChat(...);">
循环中。
我说它应该是这样的:
/*
* Write a program to determine is an entered date (day/month/year) is valid.
* As part of the program write and use the following routines:
* - validDate() which takes a year, month and day and returns whether or not
* the date is valid
* - daysInMonth() which takes a month (between 1 and 12) and year, and returns
* the number of days in the month.
* - isLeapYear() which takes a year and return whether or not it is a leap year.my solution:
*/
import java.util.*;
public class Program{
public static final int MONTH_THIRTY =30;
public static final int OTHER_MONTH = 31;
public static final int FEB =28;
public static final int LEAP_FEB = 29;
public static final int MONTHS =12;
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter date (day/month/year)");
sc.useDelimiter("/");
int day = sc.nextInt();
int month = sc.nextInt();
int year = sc.nextInt();
System.out.println(day+month+year);
validDate(day,month,year);
daysinMonth(month,year);
ifLeapYear(year);
}
public static void validDate(int date, int month, int year){
if((date>0 && date<32) && (year>0) && (month>0 && month<=12)){
System.out.println("True");
}else {
System.out.println("False");
}
}
public static int daysinMonth(int month, int year){
int daysinmonth=31;
if(year>0 && month<=MONTHS){
switch(month){
case 2 : boolean leapyear = ((year%4==0)&& (year%100!=0)||(year%400==0));
daysinmonth = leapyear ? LEAP_FEB :FEB;
break;
case 4:
case 6:
case 9:
case 11: daysinmonth = MONTH_THIRTY;
default : daysinmonth = OTHER_MONTH;
}
}
return daysinmonth;
}
public static boolean ifLeapYear(int year){
if((year%4==0)&& (year%100!=0)||(year%400==0)){
return true;
}else{
return false;
}
}
}
我认为这应该做你想要的。
如你所见,我改变了while条件:
while
。while(tries > 0 && guess != diceNumber) {
tries--;
if (guess >= 1 && guess <= 6) {
System.out.println("Incorrect Number, you have " +tries+ " more tries.");
} else if (guess >=7 || guess<=0) {
System.out.println("Number out of range, try again; you have " + tries + " more tries.");
}
if (tries > 0) {
guess = input.nextInt();
}
}
if(tries == 0 || guess != diceNumber) {
System.out.println("You Lose!!");
} else if(guess == diceNumber){
System.out.println("You Win!!");
}
。内部的tries>0
条件也会发生变化:
guess != diceNumber
。if
。guess >= 1 && guess <= 6
。