显示的消息不正确

时间:2015-07-02 10:42:24

标签: java

当用户输入一个数字(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!!");
    }
}

3 个答案:

答案 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条件:

  • 您希望用户在剩下至少1次尝试时输入另一个号码,因此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条件也会发生变化:

  • 如果输入的号码在1到6之间,您只能说它的号码不正确:guess != diceNumber
  • 如果数字不在1到6之间,您只能说它超出范围:if
  • 您不需要检查尝试次数,而while循环已经确定了。
  • 如果用户尝试离开,您只想获得一个新的输入数字:guess >= 1 && guess <= 6
相关问题