我的代码在控制台中运行时立即自动终止,Eclipse IDE,代码中没有错误

hs1ihplo  于 2022-11-23  发布在  Eclipse
关注(0)|答案(3)|浏览(182)

我正在编写一个Java程序,用于用户输入一个代表8位二进制数的字符串。如果输入有效,程序将计算1的个数,如果输入无效,程序将提示用户输入有效的数字。我在网上找到了这段代码的版本,但仍然找不到我的错误。我使用的是Eclipse IDE 2021-12和Java 17.0.1。

import java.util.Scanner;

public class Chapter6PA1 {
    
    public static void main(String[]args){
        //initialize variables and input
        Scanner sc = new Scanner(System.in);
        String word = "";//user input word
        int count=0;//the number of 1s in the word
        boolean isValid = false;//initialize boolean to be false until conditions are met for valid binary number
        do {
            for(int i=0; i <word.length()-1; i++) {
                System.out.println("Please enter a valid binary number: ");//prompts user input
                word = sc.nextLine();//determines input is equal to string word
                if(word.length()==7){
                    if(word.charAt(i)=='1'||word.charAt(i)=='0'){
                        if(word.charAt(i)=='1');
                        isValid = true;//when the above conditions are met then we have a valid binary number
                        count++;//count will increment each time a character is equal to 1
                        System.out.println("The binary number you entered contains " + count + " ones.");
                    }
                                    
                }
                else {
                    System.out.println("Invalid binary number.Please enter an 8-digit word containing only 1s and 0s.");
                    break;
                }
            
            }
        }while(isValid);
        }
    }

这就是我现在所处的位置,我在do循环中初始化了String声明,现在程序输出不正确。
代码如下:

import java.util.Scanner;

public class Chapter6PA1 {
    
    public static void main(String[]args){
        //initialize variables and input
        Scanner sc = new Scanner(System.in);
        int count=0;//the number of 1s in the word
        boolean isValid = false;//initialize boolean to be false until conditions are met for valid binary number
        do {
            System.out.println("Please enter a valid binary number: ");//prompts user input
            String word = sc.nextLine();//determines input is equal to string word//user input word
            for(int i=0; i <word.length()-1; i++) {
                if(word.length()==8){
                    if(word.charAt(i)=='1'||word.charAt(i)=='0'){
                        if(word.charAt(i)=='1');
                        isValid = true;//when the above conditions are met then we have a valid binary number
                        count++;//count will increment each time a character is equal to 1
                        System.out.println("The binary number you entered contains " + count + " ones.");
                    }
                                    
                }
                else {
                    System.out.println("Invalid binary number.Please enter an 8-digit word containing only 1s and 0s.");
                    break;
                }
            
            }
        }while(!isValid);
        }
    }

输出如下:

>Please enter a valid binary number: 
>0101
>Invalid binary number.Please enter an 8-digit word containing only 1s and 0s.
>Please enter a valid binary number: 
>01010101
>The binary number you entered contains 1 ones.
>The binary number you entered contains 2 ones.
>The binary number you entered contains 3 ones.
>The binary number you entered contains 4 ones.
>The binary number you entered contains 5 ones.
>The binary number you entered contains 6 ones.
>The binary number you entered contains 7 ones.
rvpgvaaj

rvpgvaaj1#

因为字符串字是空的,当它进入for循环时,它会检查i(为0)是否小于字符串字的长度-1(也为0),因此它不会进入for循环。
因此替换下面的行,

String word = "";//user input word

在for循环中的这些行位于循环的上方

System.out.println("Please enter a valid binary number: ");//prompts user input
           String word = sc.nextLine();//determines input is equal to string word
ckx4rj1h

ckx4rj1h2#

正如@bdavidson024所提到的,这个词最初是空的,所以循环中断而不执行。
请尝试以下代码:

public static void main(String[]args){
        //initialize variables and input
        Scanner sc = new Scanner(System.in);
        String word="";//user input word
        int count=0;//the number of 1s in the word
        boolean isValid = false;//initialize boolean to be false until conditions are met for valid binary number
        do {
            System.out.println("Please enter a valid binary number: ");//prompts user input
            word = sc.nextLine();//determines input is equal to string word
            if(word.length()==8){
                isValid = true;
                for(int i=0; i <word.length(); i++) {                
                    if(word.charAt(i)=='1'||word.charAt(i)=='0'){
                        if(word.charAt(i)=='1'){
                            count++;//count will increment each time a character is equal to 1
                        }
                    }
                    else break;
                }              
                System.out.println("The binary number you entered contains " + count + " ones.");
            }  
            else {
                System.out.println("Invalid binary number.Please enter an 8-digit word containing only 1s and 0s.");
                isValid = false;
            }
        } while(!isValid);
    }
enxuqcxy

enxuqcxy3#

这里是正确的代码@Naman只是改变循环条件!

public static void main(String[]args){
        //initialize variables and input
        Scanner sc = new Scanner(System.in);
        int count=0;//the number of 1s in the word
        boolean isValid = false;//initialize boolean to be false until conditions are met for valid binary number
        do {
            System.out.println("Please enter a valid binary number: ");//prompts user input
            String word = sc.nextLine();//determines input is equal to string word//user input word
            if(word.length()==8){
                isValid = true;//when the above conditions are met then we have a valid binary number
                for(int i=0; i <word.length()-1; i++) {
                    if(word.charAt(i)=='1'||word.charAt(i)=='0'){
                        if(word.charAt(i)=='1') {
                            count++;//count will increment each time a character is equal to 1
                        }
                    }
                    else break;             
                }
                System.out.println("The binary number you entered contains " + count + " ones.");

            }
            else {
                System.out.println("Invalid binary number.Please enter an 8-digit word containing only 1s and 0s.");
                isValid=false;
            }
        }while(!isValid);
    }
}

相关问题