Home > Back-end >  Request bosses! Why using the substring JAVA to create a new string IF judgment is an error?
Request bosses! Why using the substring JAVA to create a new string IF judgment is an error?

Time:02-12

Because I was the keyboard input number into a String type first, and then use the substring to obtain bits ten with ten digits automatically generated judgment

 import Java. Util. Scanner; 
The class lotterygame {
Public static void main (String [] args) {
Scanner variable=new Scanner (System. In);
Int bonus=(int) (Math. The random () * 90 + 10);//random in the production of 10-99 digital
String bonus1=bonus + ""//into type String
String bonusone=bonus1. Substring (0, 1);//get the generated digital bits
String bonustwo=bonus1. Substring (1, 2);//get the generated number 10

Scanner input=new Scanner (System. In);
Int inputl=input. NextInt ();//get input number
String input1=inputl + ""//into type String
String inputone=input1. Substring (0, 1);//get the generated digital bits
String inputtwo=input1. Substring (1, 0).//get the generated number 10

If (bonus1. Equals (input1)) {//I heard==judgment is not good, use the equals judgment
System. The out. Println (" 10000 ");
} else if (bonusone equals (inputtwo) & amp; & Bonustwo. Equals (inputone)) {//see the generated bits with the input of ten

If, on the other hand,,
System. The out. Println (" 3000 ");
} else if (inputone equals (bonusone) | | inputtwo. Equals (bonustwo)) {
System. The out. Println (" 1000 ");
} else if (inputone equals (bonusone) | | inputone. Equals (bonustwo) | |

Inputtwo. Equals (bonusone) | | inputtwo. Equals (bonustwo)) {
System. The out. Println (" 500 ");
} else {
System. The out. Println (" no money ");
}
System. The out. Println (" the winning Numbers: "+ bonus1);
}
}



Error message:
The Exception in the thread "main" Java. Lang. StringIndexOut
The ex out of range: 1
The at Java. Lang. String. The substring (String. Java: 1
The at lotterygame. Main (lotterygame. Java: 14)


Enter the Numbers, then an error



CodePudding user response:

Has been solved, type is caused by inconsistent problem, as long as the types of the won't error!
  • Related