为什么公共类在Java中无法工作?

huangapple go评论88阅读模式
英文:

Why won't the the public class work in Java?

问题

package finalOOP1;

import java.util.Scanner;

public class main {
    // I want to create a simple bookstore program for browsing and buying books
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        horror h1 = new horror();
        
        System.out.println("Pick a genre:");
        System.out.println("Horror");
        System.out.println("Adventure");
        System.out.println("Fantasy");
        String genre = scan.next();
        
        if (genre.equals("Horror")) {
            h1.titles();
        }
    }
}
import java.util.Scanner;

public class horror {
    
    static Scanner scan = new Scanner(System.in);
    
    public static void titles() {
        System.out.println("Pick a book number.");
        System.out.println("1. 1984");
        System.out.println("2. Goosebumps");
        System.out.println("3. Dracula");
        System.out.println("4. Go back to genre selection");
        int choose = scan.nextInt();
        
        if (choose == 1) {
            System.out.println("You chose 1984.");
        } else if (choose == 2) {
            System.out.println("You chose Goosebumps.");
        } else if (choose == 3) {
            System.out.println("You chose Dracula.");
        } else {
            main.main(null); // calling the main method from main.java
        }
    }
}

You encountered an issue where the program terminates immediately after you input "Horror." The problem lies in the way you are comparing strings in Java. Instead of using == to compare strings, you should use the equals method to compare their content.

英文:

I'm trying to make a simple bookstore program where people can browse the books and buy some if they want. First they'll choose a genre then choose a title before checking out. If they want to buy more, they'll be sent back to the genre selection.

package finalOOP1;

import java.util.Scanner;

public class main {
	//i wanna make a bookstore that sells novels from all genre
	public static void main(String[]args)
	{
		Scanner scan = new Scanner(System.in);
		horror h1 = new horror();
		
		System.out.println("Pick a genre:");
		System.out.println("Horror");
		System.out.println("Adventure");
		System.out.println("Fantasy");
		String genre = scan.next();
		
		if (genre=="Horror")
		{
			h1.titles();
		}
			
	}
}

import java.util.Scanner;

public class horror {
	
	 static Scanner scan = new Scanner(System.in);
	
	public static void titles()
	{
		System.out.println("Pick a book number.");
		System.out.println("1.1984");
		System.out.println("2.Goosebumps");
		System.out.println("3.Dracula");
        System.out.println("4.Go back to genre selection");
		int choose = scan.nextInt();
		
		if (choose == 1)
		{
			System.out.println("You chose 1984.");
		} else if (choose == 2)
		{
			System.out.println("You chose Goosebumps.");
		} else if (choose == 3)
		{
			System.out.println("You chose Dracula.");
		} else
        {
            main.main(null); //calling the main method from main.java
        }
	}
}

I haven't made the other genres coz I just wanna try one first, but when I run the main.java, I input Horror, it immediately terminate itself. It didn't show the book titles. I'm wondering if I did something wrong?

I'm new to Java, so I don't really know how this works. Isn't horror.java supposed to work like a function in c++?

答案1

得分: 2

if (genre=="Horror")

替换为

if (genre.equalsIgnoreCase("Horror"))

在Java中,字符串是对象。 "==" 检查两个对象是否相等,而不是字符是否相同。 此外,按照惯例,Java类名应以大写字母开头。

还有一些问题,我们在Java中不这样做 main.main(null); //calling the main method from main.java

英文:

Replace

if (genre=="Horror")

with

if (genre.equalsIgnoreCase("Horror"))

String in java is an object. "==" checks if both the objects are equal, not the characters are same. Also, conventionally, java class names should start with capital letter.

There are some more issues, We don't do this in java main.main(null); //calling the main method from main.java

huangapple
  • 本文由 发表于 2020年4月8日 17:12:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61097173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定