Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用try-catch做while循环_Java_Try Catch_Do While - Fatal编程技术网

Java 用try-catch做while循环

Java 用try-catch做while循环,java,try-catch,do-while,Java,Try Catch,Do While,我的代码有什么问题?我正在尝试请求提示用户输入一个数字,如果该数字不在1和6之间,或者该数字应该是无效的,并提示再次尝试。用户只能尝试3次 import java.util.Scanner; public class Game { public static void main(String[] args) { Scanner input = new Scanner(System.in); int[] numbers = new int[6]; System.out

我的代码有什么问题?我正在尝试请求提示用户输入一个数字,如果该数字不在1和6之间,或者该数字应该是无效的,并提示再次尝试。用户只能尝试3次

import java.util.Scanner;

public class Game {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] numbers = new int[6];

    System.out.println("Enter a number on a die from 1-6:");
    String dieinput = input.next();
    int invalidcount = 0;
    System.out.println("Your number is: " + dieinput);



    do{
    try
    {
      // the String to int conversion
      int dienum = Integer.parseInt(dieinput.trim());
      int dienumOE = 0;
      int count=0;

      //test number input
      if ((dienum >= 1) && (dienum<= 6))
            System.out.println("number is " + dienum + oddoreven(dienum));
      else
            System.out.println("Invalid number, please enter a number 1-6");
            count++;
            System.out.println("Count" + count);
      }
    catch (NumberFormatException nfe){
        System.out.println("Invalid number, please enter a number 1-6");
        System.out.println("Count" + count);
        }
    while (count <= 3 && count >=0);
    }
    }





// Check if odd or even
public static String oddoreven(int num) {
    String result;

    if (num % 2 == 0)
        result = "even";
    else
        result = "odd";
    return result;
}
}
import java.util.Scanner;
公开课游戏{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int[]数字=新的int[6];
System.out.println(“在模具上输入1-6之间的数字:”);
字符串dieinput=input.next();
int invalidcount=0;
System.out.println(“您的号码是:“+dieinput”);
做{
尝试
{
//字符串到int的转换
int dienum=Integer.parseInt(dieinput.trim());
int-dienumOE=0;
整数计数=0;
//测试号码输入
如果((dienum>=1)&(dienum您的问题是“错误的”范围:您不能在catch块中使用在try块范围内声明的变量

换句话说,您需要:

int counter = 0;
try { ...
} catch (... 
如果您想使用中的计数器,请尝试并捕获

“规则”非常简单:变量只有在关闭它所在的“块”的}之前才可见

这会阻止您实际编译代码

因此,这里的real课程更像是:不要写100行代码来运行编译器。只写尽可能少的行-每次当你认为这“足够”时编译..然后编译。然后修复报告给您的错误。java编译器非常擅长向您提供特定的消息,告诉您错误所在;但是您编写的代码越凌乱,就越困难


我要说的是:所以不是“遥控器”编译器服务,您应该使用它来编译凌乱的代码!

Infinite循环。您正在每个循环的开始处将count设置为0。请参阅下面的答案,但也不要在循环内将此变量设置为零。请发布完整的示例-您的源代码甚至没有编译。我更改了int计数器,但do while仍然是关于工作是否正确。建议?请注意:您不应该使用“远程”编译器服务来编译混乱的代码!这很有趣,因为您就是这么做的。这些问题有重复的,不需要再次回答。