我正在撰寫一個 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 回圈中初始化字串宣告,現在程式輸出不正確。這是代碼。
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.
uj5u.com熱心網友回復:
因為字串字是空的,當它進入 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
uj5u.com熱心網友回復:
正如@ bdavidson024已經提到的,這個詞最初是空的,所以回圈中斷而不執行。while 回圈內的條件也應該是 !isValid
試試下面的代碼:
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);
}
uj5u.com熱心網友回復:
謝謝大家這里是正確的代碼@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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/442917.html
下一篇:這個嵌套回圈如何得到這個輸出?
