[英]accessing variable within main method
我是Java的新手,編寫此程序可以隨機排列單詞並修復松弛單詞。 以下是我的程序。 調用mix()
,我希望能夠將word的輸出分配給main中的team數組。
出於某種原因,我可以調用mix()
起作用,但無法訪問shuffle函數中的單詞。 由於我在main
而所有這些功能都在main
,所以我認為我可以訪問變量。 有什么想法我在這里想念的嗎?
import java.util.Scanner;
import java.io.*;
import java.util.*;
public class Project2
{
public static void main(String[] args)
{
System.out.println("Select an item from below: \n");
System.out.println("(1) Mix");
System.out.println("(2) Solve");
System.out.println("(3) Quit");
int input;
Scanner scan= new Scanner(System.in);
input = scan.nextInt();
//System.out.println(input);
if(input==1) {
mix();
System.out.println(word);
char team[]=word.toCharArray();
for(int i=0;i<team.length;i++){
System.out.println("Data at ["+i+"]="+team[i]);
}
}
else{
System.out.println("this is exit");
}
}
static void mix()
{
String [] lines=new String[1000];//Enough lines.
int counter=0;
try{
File file = new File("input.txt");//The path of the File
FileReader fileReader1 = new FileReader(file);
BufferedReader buffer = new BufferedReader(fileReader1);
boolean flag=true;
while(true){
try{
lines[counter]=buffer.readLine();//Store a line in the array.
if(lines[counter]==null){//If there isn't any more lines.
buffer.close();
fileReader1.close();
break;//Stop reading and close the readers.
}
//number of lines in the file
//lines is the array that holds the line info
counter++;
}catch(Exception ex){
break;
}
}
}catch(FileNotFoundException ex){
System.out.println("File not found.");
}catch(IOException ex){
System.out.println("Exception ocurred.");
}
int pick;
Random rand = new Random();
pick = rand.nextInt(counter ) + 0;
System.out.println(lines[pick]);
///scramble the word
shuffle(lines[pick]);
}
static void shuffle(String input){
List<Character> characters = new ArrayList<Character>();
for(char c:input.toCharArray()){
characters.add(c);
}
StringBuilder output = new StringBuilder(input.length());
while(characters.size()!=0){
int randPicker = (int)(Math.random()*characters.size());
output.append(characters.remove(randPicker));
}
String word=output.toString();
}
}
使用return語句從shuffle()方法返回字符串值:
static String shuffle(String input) {
// . . .
return output.toString();
}
...然后混合使用:
String word = shuffle(lines[pick]);
但是最好在編程之前先閱讀基本的Java教程 。
在Java中,無法在初始化變量的方法之外看到變量。例如,如果我聲明int foo = 3;
在主要,然后我嘗試從另一種方法訪問foo
,它將無法正常工作。 從另一種方法的角度來看, foo
甚至不存在!
在方法之間傳遞變量的方法是使用return <variable>
語句。 程序到達return
語句后,該方法將退出,並將return
之后的值(可能是foo
)返回給調用方方法。 但是,必須在聲明該方法時說該方法返回一個變量(並說明類型是什么)(就像當該方法不返回任何內容時您需要聲明void
一樣)。
public static void main(String[] args){
int foo = 2;
double(foo); //This will double foo, but the new doubled value will not be accessible
int twoFoo = double(foo); //Now the doubled value of foo is returned and assigned to the variable twoFoo
}
private static int double(int foo){//Notice the 'int' after 'static'. This tells the program that method double returns an int.
//Also, even though this variable is named foo, it is not the same foo
return foo*2;
}
另外,您可以使用實例變量來獲取類中所有方法均可訪問的變量,但是如果您不熟悉Java,則在開始學習面向對象編程的基礎知識之前,應該避免使用這些變量。
希望這可以幫助! -英國騎士
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.