我的测验程序中有多个编译器错误

时间:2016-01-25 12:32:41

标签: java

这是一个用于进行测验的java程序,你有10个问题,如果你得到一个答案正确,你会得到1分,但是没有负面标记,当我编译它时会抛出这个:' else&# 39;没有'如果'错误

import java.util.Scanner;

/**
 * Write a description of class program1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Project
{
    public static void main(String[]args)
    {
        Scanner sc = new Scanner(System.in);
        char ans;
        int score=0;

        System.out.println("1.What was the first mouse created?");
        System.out.println("(a)Glass ");
        System.out.println("(b) Wood");
        System.out.println("(c) Steel");
        System.out.println("(d) Paper");
        System.out.print("Enter Your Choice => ");
        ans=sc.next().charAt(0) ;
        if(ans=='b') {
            System.out.println("That's correct!");
            score+=1;
        }
        else {

            System.out.println("Sorry it is wrong...");
        }



        System.out.println("2. Who is the father of the 'Internet'?");
        System.out.println("(a)Alan Peris ");
        System.out.println("(c) Vint Cerf");
        System.out.println("(d) Steve Lawrence");
        System.out.print("Enter Your Choice => ");
        ans=sc.next().charAt(0) ;
        if(ans=='c')
            System.out.println("That's correct!");
        score+=1;
        else

            System.out.println("Sorry it is wrong...");




        System.out.println("3.What search engine provides "instant answers" for certain types of queries?");
        System.out.println("(a)Google ");
        System.out.println("(b) Yahoo");
        System.out.println("(c) Bing");
        System.out.println("(d) Dogpile");
        System.out.print("Enter Your Choice => ");
        ans=sc.next().charAt(0) ;
        if(ans=='c')
            System.out.println("That's correct!");
        score+=1;
        else

            System.out.println("Sorry it is wrong...");





        System.out.println("3.4K might be used synonymously with what acronym?");
        System.out.println("(a)UHD ");
        System.out.println("(b) VGA");
        System.out.println("(c) PCI");
        System.out.println("(d) HDR");
        System.out.print("Enter Your Choice => ");
        ans=sc.next().charAt(0) ;
        if(ans=='a')
            System.out.println("That's correct!");
        score+=1;
        else

            System.out.println("Sorry it is wrong...");





        System.out.println("4. A zero day exploit is a type of what?");
        System.out.println("(a) Malware ");
        System.out.println("(b) Shareware");
        System.out.println("(c) Freeware");
        System.out.println("(d) Adware");
        System.out.print("Enter Your Choice => ");
        ans=sc.next().charAt(0) ;
        if(ans=='a')
            System.out.println("That's correct!");
        score+=1;
        else

            System.out.println("Sorry it is wrong...");






        System.out.println("5.What adjective describes an image that only contains         shades of gray?");
        System.out.println("(a) Saturated ");
        System.out.println("(b) Grayscale");
        System.out.println("(c) Hueless");
        System.out.println("(d) Black and White");
        System.out.print("Enter Your Choice => ");
        ans=sc.next().charAt(0) ;
        if(ans=='b')
            System.out.println("That's correct!");
        score+=1;
        else

            System.out.println("Sorry it is wrong...");


        System.out.println("6.What does it mean if a device is erg               onomic?");
        System.out.println("(a) It is upgradeable");
        System.out.println("(b) It is enviromentally friendly");
        System.out.println("(c) It is compatible with multiple               platforms");
        System.out.println("(d) It is designed to be comfortable to use");
        System.out.print("Enter Your Choice => ");
        ans=sc.next().charAt(0) ;
        if(ans=='d')
            System.out.println("That's correct!");
        score+=1;
        else

            System.out.println("Sorry it is wrong...");



        System.out.println("7. Which of the following can be synced with iCloud?");
        System.out.println("(a) Reminders ");
        System.out.println("(b) Contacts");
        System.out.println("(c) Calendar");
        System.out.println("(d) Websites");
        System.out.print("Enter Your Choice => ");
        ans=sc.next().charAt(0) ;
        if(ans=='d')
            System.out.println("That's correct!");
        score+=1;
        else

            System.out.println("Sorry it is wrong...");





        System.out.println("8.What is the term "Wi-Fi" short for?");
        System.out.println("(a) Wireless Fidelity");
        System.out.println("(b) Wireless Finder");
        System.out.println("(c) Wireless Frequency Inte                      lligence");
        System.out.println("(d) Nothing");
        System.out.print("Enter Your Choice => ");
        ans=sc.next().charAt(0) ;
        if(ans=='d')
            System.out.println("That's correct!");
        score+=1;
        else

            System.out.println("Sorry it is wrong...");

        System.out.println("9. What do keystrokes measure?");
        System.out.println("(a) Login attempts");
        System.out.println("(b) Secure socket connections");
        System.out.println("(c) Keys pressed on a keyboard");
        System.out.println("(d) Nothing");
        System.out.print("Enter Your Choice => ");
        ans=sc.next().charAt(0) ;
        if(ans=='c')
            System.out.println("That's correct!");
        score+=1;
        else

            System.out.println("Sorry it is wrong...");




        System.out.println("10.The veronica search engine is used to search what service?");
        System.out.println("(a) Gopher");
        System.out.println("(b) Telnet");
        System.out.println("(c) BBS");
        System.out.println("(d) FTP");
        System.out.print("Enter Your Choice => ");
        ans=sc.next().charAt(0) ;
        if(ans=='a')
            System.out.println("That's correct!");
        score+=1;
        else

            System.out.println("Sorry it is wrong...");

        System.out.println("Your total score is:"+score);
    }
}
}

2 个答案:

答案 0 :(得分:1)

你有两个或多个陈述的多个ifs。

如果每个if有多个语句,则必须在此语句周围设置括号。例如:

if(ans=='b') { // Missing
            System.out.println("That's correct!");
            score+=1;
        } // Missing
        else { // Should be here but not have to, because you only have one statement here.
            System.out.println("Sorry it is wrong...");
        } 

附上您的陈述使代码更具可读性。尽管事实上它在某些时候是不必要的。

代码中的每个if语句都会重复相同的错误。你必须在每个if语句上修复它。

您所做的其他错误未在字符串中转义"。假设你有一个字符串

String myString = "Hello World";

现在你要写Hello "the one and only" World。所以你必须逃脱所有“在字符串中:

String myString = "Hello \"the one and only\" World";

答案 1 :(得分:0)

你忘记了括号:

if(ans=='b'){
    System.out.println("That's correct!");
    score+=1;
}
else
    System.out.println("Sorry it is wrong...");