Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BlueJ(如何在if-else中给出字符串值)_Java_String - Fatal编程技术网

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"))
    {