简体   繁体   English

我在 if 循环之前声明了我的变量,但我无法在循环之外访问它?

[英]I declared my variable before the if-loop but I can't access it outside of the loop?

So I need to use my variable outside of the loop but it gets its value from inside one of the if-loops but I keep getting this error:所以我需要在循环之外使用我的变量,但它从 if 循环之一内部获取它的值,但我不断收到此错误:

variable newLastName is already defined in method main(java.lang.String[])变量newLastName已经在方法main(java.lang.String[])

But I never get that error for the variable newFirstName .但是我从来没有收到变量newFirstName错误。

import java.util.Scanner;

public class Meme
{
    public static void main (String[] args)
    {
       Scanner scan = new Scanner (System.in);

       System.out.println("Please enter your first name.");
       String firstName = scan.nextLine();
       char firstChar = firstName.charAt(0);
       System.out.println("Please enter your last name.");
       String lastName = scan.nextLine();
       char lastChar = lastName.charAt(0);

       String newFirstName = null;

       if (firstChar =='A')
       {
         String newfirstname = new String("Applebees");
       }
       if (firstChar =='B')
       {
         String newfirstname = new String("Btchstick");
       }  
       if (firstChar =='C')
       {
         String newfirstname = new String("Cathy");
       }
       if (firstChar =='D')
       {
         String newfirstname = new String("Donger");
       }
       if (firstChar =='E')
       {
         String newfirstname = new String("Egg");
       }

       String newLastName = null;

       if (lastChar =='A')
       {
           String newLastName = new String("Ant Hill Supreme");
       }
       if (lastChar =='B')
       {
           String newLastName = new String("Blue Jay Junior");
       }
       if (lastChar =='C')
       {
           String newLastName = new String("...Catwoman?");
       }
       if (lastChar =='D')
       {
           String newLastName = new String("Dipstick");
       }
       if (lastChar =='E')
       {
           String newLastName = new String("Egg-Egg");
       }

       System.out.println("Your new name is: " + newFirstName + " " + newLastName);
    }
}

You're re-declaring it inside the if block.你再宣布它是否块 Don't do this.不要这样做。

Change改变

String newFirstName = null;

if (firstChar =='A')
{
  // the variable below is declared inside the if block and is
  // thus local to and visible only in the if block
  String newfirstname = new String("Applebees");
}

to

String newFirstName = null;

if (firstChar =='A')
{
  newFirstName = "Applebees";
}

Edit: as per paisanco 's comment:编辑:根据paisanco的评论:

Plus there is a typo, newFirstName outside the block, newfirstname inside the block另外有一个错字,块外的newFirstName,块内的newfirstname

Edit 2: as per elliott-frisch 's comment:编辑 2:根据elliott-frisch的评论:

Please don't use new String(...) .请不要使用new String(...)

This creates new objects needlessly by preventing Java from using the String pool when String pool.这通过阻止 Java 在 String 池时使用 String 池来不必要地创建新对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不能修改在所述循环内的循环外声明的LinkedList? - Why can't I modify a LinkedList declared outside a loop inside said loop? 为什么不能在循环外访问2D数组? - Why can't I access my 2D array outside of the loop? 如何使用我在其循环外初始化的变量? - How can I use the variable I initialized outside its loop? Java-为什么我不能在for循环之外初始化变量的起始值? - Java - Why can't I initialize the start value of a variable outside of a for loop? 如何在外部循环中从[0到9]访问字符串列表? - How can I access the Strings list from [0 to 9] outside for loop? 如何从循环外部访问在嵌套循环中声明或初始化的数组或变量? - How do you access an array or variable declared or initialized in a nested loop from outside of the loop? 为什么我不能访问在开关中声明的变量? - Why can't I access a variable declared in a switch? 在 For 循环外部声明的变量与在内部声明的变量 - Variable Declared Outside of For Loop Vs Inside 更改已在循环外声明的变量 - changing a variable that has been declared outside of a loop JavaFX-线程挂起,我不知道如何在UI线程外运行循环 - JavaFX - Thread hangs, and I can't figure out how to run my loop outside the UI thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM