Java:错误,"变量species1已在方法name1()"

时间:2016-10-27 12:35:23

标签: java

我正在编写一个相当简单的程序,只是为了学习基础知识 在此,用户可以提高'一只宠物龙,所以它询问用户想要什么物种,如果用户需要帮助,可以帮助物种名称,然后询问名称。
然后它给龙一个随机的渴望 它是按预期工作的,直到我添加了if语句和种类选项。

现在我得到2个我无法解决的错误:

Project.java:62: error: variable species1 is already defined in    method name1()           default: String species1;
                            ^ Project.java:74: error: cannot find symbol        System.out.println("Congratulations, " + name + " the " + species1
+ " has been born!");
                                                                  ^   symbol:   variable species1   location: class Project 2 errors***

任何帮助将不胜感激。

import java.util.Scanner;
import java.util.Random;

class Project {



    public static void main(String[] args) {

        intro();
        String s1 = name1();
        thirst(s1);

        System.exit(0);

    }


//The following method explains what the program is for. 

    public static void intro ()  {

        System.out.println("This program allows you to keep and raise a Dragon!");

        return;
    }




//The following method allows the user to name their pet Dragon and select its species. 

    public static String name1 ()  {

        Scanner dragonSpecies = new Scanner(System.in);

        System.out.println("What species of Dragon would you like to raise? You may type 'help' to get a list of existing dragon species.");    

        String species = dragonSpecies.nextLine();


        switch(species) { 
            case "help": 
                System.out.println("Anglewing: The Anglewing is a smaller dragon with golden brown oddly angled wings and bright yellow spots.");
                System.out.println("");
                System.out.println("Regal Copper: This heavyweight dragon exceeds all known breeds in sheer size (30–50 tons). Regal Coppers have vivid colorations ranging from red to yellow, and are very far-sighted.");
                System.out.println("");
                System.out.println("Malachite Reapers: Cousins of the Yellow Reapers, with yellow-brown coloring with green highlights. Prefer the cooler climates of northern Scotland and mid-size.");
                System.out.println("");
                System.out.println("Sharpspitter: A venomous breed considered too small for military use, that was cross-bred with the larger French Honneur-d'Or and the venomous Russian Ironwing to create the Longwings.");
                System.out.println("");
                System.out.println("Chequered Nettle: As a heavyweight dragon, the Chequered Nettle is very useful in battle, as they possess spiked tails that can be used in a manner similar to morning stars. The colouring a Chequered Nettle is gold with brown stripes."); 
                System.out.println("");
                Scanner dragonSpecies1 = new Scanner(System.in);            
                System.out.println("What species of Dragon would you like to raise?");  

                String species1 = dragonSpecies1.nextLine();
                break;
            default: 
                String species1;
                species1 = species;
            break;
        }


        Scanner dragonName = new Scanner(System.in);

        System.out.println("What Would you like to name your pet Dragon?");

        String name = dragonName.nextLine();

        System.out.println("Congratulations, " + name + " the " + species1 + " has been born!");

        return name;  
    }



//The following method returns the thirst value of the dinosaur as a random number between 1 and 10 out of 10

     public static void thirst(String name) {

        Random thirst = new Random();


        int thirstLevel = thirst.nextInt(10) + 1;

        System.out.println(name + "'s thirst level is " + thirstLevel + "/10");

        return; 
    }

}

2 个答案:

答案 0 :(得分:1)

删除id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2506 Using temporary; Using filesort 2 DERIVED shop_order_basket index product_id order_id_2 8 NULL 357289 Using where; Using index 3 DEPENDENT SUBQUERY shop_order_basket ref order_id,product_id,order_id_2 order_id 4 func 1 Using where 3 DEPENDENT SUBQUERY shop_order eq_ref PRIMARY,entry_datetime,id PRIMARY 4 func 1 Using where 变量。这是不必要的。只需指定并使用species1值即可。

您还只需要一个species个变量,而不是每个提示一个变量。

换句话说,删除默认情况,将此行放在帮助案例中

Scanner

打印时使用species = dragonSpecies.nextLine(); 和名称。

(可选)将species重命名为dragonSpecies或其他内容,然后删除其他input个对象。然后所有提示都使用Scanner

答案 1 :(得分:0)

你宣布了两次:

        Scanner dragonSpecies1 = new Scanner(System.in);            
        System.out.println("What species of Dragon would you like to raise?");  

        String species1 = dragonSpecies1.nextLine(); // <------------ HERE
        break;
        default: String species1;  // <------------ AND HERE
            species1 = species;

您在此处并不需要新变量,可以重复使用species。但是,如果在另一种情况下你真的需要一个新的,尝试像

这样的东西
    String species1;
    switch(species) { 
        case "help": System.out.println("Anglewing: The Anglewing is a smaller dragon with golden brown oddly angled wings and bright yellow spots.");
        System.out.println("");
        System.out.println("Regal Copper: This heavyweight dragon exceeds all known breeds in sheer size (30–50 tons). Regal Coppers have vivid colorations ranging from red to yellow, and are very far-sighted.");
      ...
        System.out.println("");
        Scanner dragonSpecies1 = new Scanner(System.in);            
        System.out.println("What species of Dragon would you like to raise?");  

        species1 = dragonSpecies1.nextLine();
        break;
    default: 
        species1 = species;
        break;