The method If(boolean) is undefined for the type

北战南征 提交于 2019-12-13 08:59:35

问题


Eclipse keeps giving me the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
input cannot be resolved
The method If(boolean) is undefined for the type bai1DinhLuatCuLong
Syntax error, insert ";" to complete Statement
F cannot be resolved to a variable

I do not understand this. What do I need to fix? Why can't I use the If() function?

I've tried looking through Google and Bing, and I've double checked my Java examples book, but I could not work this out.

import java.util.Scanner;


public class bai1DinhLuatCuLong
{
    public static void main(String[] args)
    { 
    System.out.print("Giá trì cần tìm (F // q // r) : ");   
    char cantim = input.nextChar();

            // HERE THIS IS WHERE I PUT THE IF STATEMENT !!!
    If (cantim == 'F') {

        Scanner input = new Scanner(System.in); 
        System.out.print("Độ lớn của điện tích thứ nhất : ");
        double q1 = input.nextDouble();
        System.out.print("Độ lớn của điện tích thứ hai : ");
        double q2 = input.nextDouble();
        System.out.print("Khoảng cách giữa 2 điện tích : ");
        double r = input.nextDouble();
        System.out.print("Hệ số k sẽ tự động được đặt là 9*10^9 như trong hệ SI");
        double F = 9 * Math.pow(10,9) * Math.abs(q1) * Math.abs(q2) / Math.pow(r,2);
    }

    System.out.print("Độ lớn lực tương tác giữa 2 điện tích điểm : " + "\n Lưu ý E là *10");

    }
}

回答1:


If does not have a large cap I in java: it should be if.




回答2:


if (cantim == 'F') it should be small i

or

You need to write a method

public boolean If(boolean test)
{
//Your logic.
}

But, based on your code, it seems you are looking for "if" statement than having new method.




回答3:


Maybe You want to do something like this:

Scanner input = new Scanner(System.in);
String cantim = input.next();

if (cantim.equals("F")) {
    System.out.println("If F");
} else {
    System.out.println("Out of F");
}


来源:https://stackoverflow.com/questions/11904856/the-method-ifboolean-is-undefined-for-the-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!