[英]Making a word scrambler in java
import java.util.Scanner;
public class WordScrambler
{
public String prefix, inner, postfix, newword;
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
String words = sc.nextLine();
System.out.println(words);
}
public void Scrambler()
}
String [] words = words.split(" ");
}
} }
I'm not really understanding how to get my variable "words" into my scramble method so i can split each word that i put into seperate strings, am i just declaring a new array words when i do this? 我不是很了解如何将我的变量“单词”放入我的加扰方法中,因此我可以将输入的每个单词拆分为单独的字符串,这样做时是否只是在声明一个新的数组单词? How do i grab that variable from above.
我如何从上方获取该变量。 Also, my system.out.println is just checking to see if my scanner worked.
另外,我的system.out.println只是检查我的扫描仪是否工作。 Or am i doing it right and is it actually splitting the words?
还是我做对了,实际上是分开的话吗?
It seems that you simply want to pass a variable from one method to another. 似乎您只是想将变量从一种方法传递给另一种方法。
sidenote: in Java, method names begin with a lowercase letter and are usually verbs: scramble not Scrambler 旁注:在Java中,方法名称以小写字母开头,通常是动词:加扰而不是加扰
In: 在:
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
String words = sc.nextLine();
System.out.println(words);
}
You've gotten the input, but still don't have anywhere to send it because you haven't created an instance of WordScrambler yet. 您已经获得了输入,但是仍然没有任何地方可以发送它,因为您尚未创建WordScrambler的实例。
So, let's make one of those: 因此,让我们做一个:
WordScrambler wordScrambler = new WordScrambler();
Okay, now you have access to the scramble method, so 好的,现在您可以访问加扰方法了,所以
wordScrambler.scramble( words )
Will get things moving 会让事情动起来
Now, you'll need to do something with that String in scramble (after you fix the method declaration to accept a String parameter), like split it, jumble the words and output the result, for instance. 现在,您需要对字符串进行一些打乱(在修复方法声明以接受String参数之后),例如将其拆分,混杂单词并输出结果。
import java.util.Scanner;
public class WordScrambler {
public String prefix, inner, postfix, newword;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String words = sc.nextLine();
WordScrambler wordScrambler = new WordScrambler();
wordScrambler.scrambler(words);
}
public void scrambler(String words) {
String[] word = words.trim().split(" ");
for (int i = 0; i < word.length; i++) {
System.out.println(word[i]);
}
}
}
You need to pass the variable to the method as a parameter. 您需要将变量作为参数传递给方法。 If you make the method
static
, you can call it simply from your main()
. 如果将方法
static
,则可以从main()
调用它。
Also, name your input as (obviously) input
, because that's what it is. 同样,将您的输入命名为(显然)
input
,因为这就是它的意思。
Here's how I would do it: 这是我的处理方式:
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
String input = sc.nextLine();
System.out.println(scramble(input));
}
public static String scramble(String input) {
List<String> words = new ArrayList<String>(Arrays.asList(input.trim().split("\\s+")));
Collections.shuffle(words);
return words.toString().replaceAll("[^\\w ]", "");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.