Ee

56 2

The code isn't long, please. Please check the mistake. I'd like to print pro1 input pro2 when I'm trying to enter pro1. When I enter, I quit the program and can loop input straight straight to exit the actual operation whether it's"reinput", and know that it's"", and don't know how to do this.

import java.io.*;
public class TestWhile{
 public static void main(String[] args){
 try{
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 String s = br.readLine();
 while(s!="exit"){
 while(s =="pro1"){
 System.out.println("pro1");
 s = br.readLine();
 }
 while(s =="pro2"){
 System.out.println("pro2");
 s = br.readLine();
 }
 while((s!="pro1")&&(s!="pro2")&&(s!="exit")){
 System.out.println("Reinput");
 s = br.readLine();
 }
 }
 }
 catch(IOException e){
 e.printStackTrace();
 }
 System.out.println("The end");
 }
 }
2 Answers

108 2

welcome to the java world, the first program isn't.

A key problem with your program is that in java, you can't use == numbers to determine whether two strings are equal ( this isn't the same as you in other scripting languages ), because is a strong type of language, which is really not equal to whether it's equal to two, and is certainly not equal to two.

As a result, the solution is simple to determine whether a string a is equal to b, please use a.equals(b). is a frequent mistake, sometimes I write the inertial scripting language and write java that will make this mistake, and you can't see the error.

65 5

welcome to the java world, the first program isn't.

A key problem with your program is that in java, you can't use == numbers to determine whether two strings are equal ( this isn't the same as you in other scripting languages ), because is a strong type of language, which is really not equal to whether it's equal to two, and is certainly not equal to two.

As a result, the solution is simple to determine whether a string a is equal to b, please use a.equals(b). is a frequent mistake, sometimes I write the inertial scripting language and write java that will make this mistake, and you can't see the error.

76 3

In addition to joyqi 's string contrast problem, the logical judgment of the program should be more appropriate with if.

import java.io.*;
public class TestWhile
{
 public static void main(String[] args)
 {
 try {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 String s = br.readLine();
 while (!s.equals("exit")) {
 if (s.equals("pro1")) {
 System.out.println("pro1");
 } else if (s.equals("pro2")) {
 System.out.println("pro2");
 } else {
 System.out.println("Reinput"); 
 }
 s = br.readLine();
 }
 } catch(IOException e) {
 e.printStackTrace();
 }
 System.out.println("The end");
 }
}
110 1

In addition to joyqi 's string contrast problem, the logical judgment of the program should be more appropriate with if.

import java.io.*;
public class TestWhile
{
 public static void main(String[] args)
 {
 try {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 String s = br.readLine();
 while (!s.equals("exit")) {
 if (s.equals("pro1")) {
 System.out.println("pro1");
 } else if (s.equals("pro2")) {
 System.out.println("pro2");
 } else {
 System.out.println("Reinput"); 
 }
 s = br.readLine();
 }
 } catch(IOException e) {
 e.printStackTrace();
 }
 System.out.println("The end");
 }
}
...