我是Java的新手,编写这个程序来改变单词并修复单词。以下是我的计划。在我调用mix()
之后,我希望能够将单词的输出分配给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();
}
}
答案 0 :(得分:2)
使用return语句从shuffle()方法返回字符串值:
static String shuffle(String input) {
// . . .
return output.toString();
}
...然后在mix中使用它:
String word = shuffle(lines[pick]);
但在编程之前最好read basic java tutorials。
答案 1 :(得分:1)
在Java中,变量不能在它们初始化的方法之外看到。例如,如果我在main中声明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新手,则在开始学习面向对象编程的基础知识之前,应该避免这些变量。
希望这有帮助! -BritKnight