Java BlueJ(如何在if-else中给出字符串值)
这个程序有什么问题 我想创建一个只在输入密码时打开的程序。 它不接受字符串值作为密码。那么有人能修改它吗Java BlueJ(如何在if-else中给出字符串值),java,string,Java,String,这个程序有什么问题 我想创建一个只在输入密码时打开的程序。 它不接受字符串值作为密码。那么有人能修改它吗 import java.io.*; class aa { public static void main(String input)throws IOException { String a; if(a.equals("A")) { InputStreamReader read = new Inpu
import java.io.*;
class aa
{
public static void main(String input)throws IOException
{
String a;
if(a.equals("A"))
{
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
double x;
double y;
double z;
System.out.println("Please type the two Numbers");
x=Double.parseDouble(in.readLine());
y=Double.parseDouble(in.readLine());
z=x*y;
System.out.println("Product="+z);
}
else
{
System.out.println("Wrong Password");
}
}
}
您尚未为变量a指定任何值。因此,它永远不会进入if循环。试试这个:
String a;
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
a = in.ReadLine();
if(a.equals("A"))
{
...
...
}
您必须添加读取字符串的代码才能读取字符串 例如:
public static void main(String input)throws IOException
{
String a;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // add this line
a = br.readLine(); // add this line
if(a.equals("A"))
{
或者如果您打算使用输入
:
public static void main(String input)throws IOException
{
String a;
a = input; // add this line
if(a.equals("A"))
{
注意:此main
方法不是入口点,因为入口点的签名是publicstaticvoidmain(String[]args)
,此函数的签名与此不同,因为参数不是数组而是字符串。a.equals()
在a
未初始化时使用。在使用之前初始化它。
public static void main(String input)throws IOException
{
String a;
a = input; // add this line
if(a.equals("A"))
{