2015-12-30 42 views
-1

import java.util.Scanner;如何獲取代碼並將其插入到打印語句中?

公共類權重{

public static void main(String[] args) { 
    //Make scanner to allow user input 
    Scanner input = new Scanner (System.in); 
    //Ask for gender 
    System.out.println("What is your gender (Male/Female)?: "); 
    //Store gender as string 
    String gender = input.nextLine(); 
    //Ask for weight 
    System.out.println("What is your weight (to closest lb)?: "); 
    //Stores weight as integer 
    int userWeight = input.nextInt(); 

} 

public void genderAndWeight (String gender, int userWeight) { 
    //int femaleWeight = 0; 
    int maleWeight = 30; 
    //Determine whether to use male or female int variable 

    if(gender.equalsIgnoreCase("male")) 
     { 
     userWeight -= maleWeight; 
     } 
} 
public String conditions(int userWeight) 
{ 

    if(userWeight<100) 
    { 
     String X = " underweight"; 


    } 
    else if(100 <= userWeight && userWeight <= 130) 
    { 
     String X = " average weight"; 

    } 
    else if (userWeight > 130) 
    { 
     String X = " overweight"; 

    } 
    else System.out.println("You suck at typing."); 

    System.out.println("You are" + X); 
} 

}

我試圖使此代碼找人是否在,平均或超重,但我似乎無法弄清楚如何正確使用該變量。

+0

我不知道該代碼甚至會編譯。 – csmckelvey

回答

0

既然你已經在你的主要功能和功能genderAndWeight和條件變量,當你打電話與您主要有變量的函數,會發生什麼?

conditions(userWeight); 
genderAndWeight(gender, userWeight); 
1

您的方法應該可能是static(否則,它們需要類的一個實例)。無論如何,和你的問題,你需要確保X範圍(所以它是可見的)。喜歡的東西,

public static String conditions(int userWeight) 
{ 
    String X = ""; // <-- declare and initialize X 
    if(userWeight<100) { 
     X = " underweight"; 
    } else if(userWeight <= 130) { 
     X = " average weight"; 
    } else if (userWeight > 130) { 
     X = " overweight"; 
    } else { 
     System.out.println("You suck at typing."); 
     X = " unknown"; 
    } 
    System.out.println("You are" + X); 
    return X; // <-- you need to return String. 
} 
0

你裏面你如果實際上是創建新的變數,每次它走出去的範圍報表一旦你離開了,如果塊。爲了修復你的代碼,刪除你的X聲明並把所有的if語句放在外面,並確保你的方法聲明是靜態的。像這樣...

public static String conditions(int userWeight) 
    { 
      String X = ""; 

      // and then you continue with you if statements and make sure not to redeclare X, just use it 
    } 

您需要了解java中的範圍界定。變量作用域是該變量可以訪問的程序區域。當你聲明一個類的時候,你聲明的任何變量都有一個類作用域(這裏的類作用域並不是指靜態變量,我的意思是它可以被該類中的所有方法訪問)。如果你在一個方法中聲明瞭一個變量,它就被稱爲一個局部變量,並且它只被該方法所知。如果您嘗試在另一種方法中調用該變量,則會出現錯誤。此外,還有塊範圍,也就是說,在塊中聲明的任何變量只能被該塊訪問,即使在同一個方法中,該塊之外的代碼也不能訪問該變量。局部變量聲明也放在使用它們的語句之前。讓我用實例證明

public class Test 
    { 
     int classScope;// this variable has class scope, it can be accessed by all methods in this class 

     public void someMethod() 
     { 
       int localScope = 1;// this is a local variable, it is only accessible inside this method 

       if (true) 
       { 
         int blockScope = 0; // this variable has block scope 
       } 
       System.out.println(blockScope); // this will cause an error because variable blockScope is not visible here 
     } 

     public void anotherMethod() 
     { 
       System.out.println(localScope); // this will cause an error, because variable localScope is not visible here. 
     } 
    } 

注意:在使用之前,局部變量必須初始化,而不是它有一個默認值的實例變量。

+0

感謝您打破這一局面,仍然習慣於Java! – user5716047

+0

很高興我能幫上忙 – ivange