创建Java方法
我正在创建一个与另一个文件配合运行的java程序,以帮助创建所谓的面向对象编程 所以我已经完成了整个程序,但是我很难理解如何编译和运行它。我知道我的问题在方法的名称范围内。然而,我不明白如何知道该调用什么方法。从我所读到的有几种不同的类型,你可以称之为 (我也知道缩进看起来很糟糕,在我的实际程序中是正确的) 以下是我所看到的,以帮助理解我的问题:创建Java方法,java,methods,Java,Methods,我正在创建一个与另一个文件配合运行的java程序,以帮助创建所谓的面向对象编程 所以我已经完成了整个程序,但是我很难理解如何编译和运行它。我知道我的问题在方法的名称范围内。然而,我不明白如何知道该调用什么方法。从我所读到的有几种不同的类型,你可以称之为 (我也知道缩进看起来很糟糕,在我的实际程序中是正确的) 以下是我所看到的,以帮助理解我的问题: import java.util.Scanner; import java.util.Random; public class RockPa
import java.util.Scanner;
import java.util.Random;
public class RockPaperScissors
{
public static void main(String[] args)
{
class getUserChoice
{
System.out.println("Would you you like to play rock, paper scissors?");
userChoice = in.nextLine();
while (!userChoice.equalsIgnoreCase("Rock") && !userChoice.equalsIgnoreCase("Paper") && !userChoice.equalsIgnoreCase("Scissors"))
{
System.out.println("Invalid entry please try again");
userChoice = in.nextLine();
}
userChoice = userChoice.toUpperCase();
return userChoice;
} //end of UserChoice
class getCPUChoice
{
computerRandom = r.nextInt(3)+1;
if (computerRandom == 1) cpuChoice = "Rock";
else if (computerRandom == 2) cpuChoice = "Paper";
else if (computerRandom == 3) cpuChoice = "Scissors";
}
class pickWinner
{
//tie
if (userChoice.equals(cpuChoice))
{
winner = tie;
}
//user has rock
if (userPlay.equals("rock"))
{
if (cpuChoice.equals("Scissor"))
{
winner = user;
}
else if (cpuChoice.equals("Paper"))
{
winner = Computer;
}
else if (cpuChoice.equals("Rock"))
{
winner = tie;
}
}
if (userPlay.equals("Paper"))
{
if (cpuChoice.equals("Scissor"))
{
winner = Computer;
}
else if (cpuChoice.equals("Paper")
{
winner = tie;
}
else if (cpuChoice.equals("Rock")
{
winner = user;
}
}
if (userPlay.equals("Scissors"))
{
if (cpuChoice.equals("Scissor"))
{
winner = tie;
}
else if (cpuChoice.equals("Paper")
{
winner = user;
}
else if (cpuChoice.equals("Rock")
{
winner = computer;
}
}
}
}//剪刀的末端
}
我的错误是:
RockPaperScissors.java:10:错误:应为
你想玩石头、剪纸吗
据我所知,这是因为课程设置不正确。也许有人能帮你解释一下你是如何知道正确命名每个方法的
谢谢 一个文件中不能声明多个
public
类
要解决此问题,请删除单词public
然而,我不明白如何知道该调用什么方法
对于这一点,要调用方法,只需使用方法名称(我答案的完整示例):
这不是有效的Java。创建类不是为了获得选择;让它成为一种方法
public class RockPaperScissors {
private static final String [] choices = { "ROCK", "PAPER", "SCISSORS" };
private Random random;
public RockPaperScissors() { this(null); }
public RockPaperScissors(Long seed) {
this.random = (seed == null) ? new Random() : new Random(seed.longValue());
}
// An Enum would be better than a number or String
public String nextChoice() {
return choices[this.random.nextInt(choices.length)];
}
}
类和方法可以在其他类中定义,但都不能在方法(函数)中定义,匿名函数除外。如果这是一个您打算编写的独立程序,并且您打算将
getUserChoice
和getCPUChoice
作为方法,只需删除这两个名称前的class
关键字,如果您希望它们成为RockPaperScissors
类的静态方法,则可以将它们替换为static
。方法需要有一个返回类型,在您的情况下,它可以是String
。您仍然需要在main
方法中调用任何一种方法。我想这就是您在“标准”程序中寻找的那种结构。您可以在其他文件中声明其他类,也可以导入这些类。您的代码通常在这些类中的方法中运行,并且都是从作为入口点的主方法调用的
//imports here
public class ClassName {
public static void main(String[] args) {
//main entry point to your program
}
private String methodName(String param1) {
//do some stuff here
return someValue;
}
}
不能在一个文件中创建多个类。所以这里应该只有一个“class”关键字 您的方法没有很好的格式化,您应该看看Java入门指南:) 下面是class+方法的一个简单示例:
public class MyClass{
//Attribute
public int myMethod(int x) {
return x+1;
}
}
方法由其可见性(
public
)、返回类型(int
)、名称(myMethod
)及其参数(intx
)定义。因此,在这里,您的方法有一个“类”返回类型,这是不可能的。似乎您正在尝试编写用户和计算机之间的某种猜测游戏……很好,但没有听起来那么简单。
正如许多人所说,您遇到了问题,因为您声明的类具有错误的可见性,并且不完全在正确的位置
你给两个类起的名字让我觉得它们应该是方法而不是类。所以两个有三个方法会更简单
- 主要方法:这就是你运行游戏循环的地方
- getUserChoice():这是要求用户输入其选择的地方(最好阻止用户输入有效的选择)
- getCPUChoice:在这里,您要求cpu输入他的选择
public class RockPaperScissors {
static String userChoice = "";
static String cpuChoice = "";
static Scanner in = new Scanner(System.in);
static Random r = new Random();
// ... rest of the code here
在使用随机对象之前,您应该实例化随机对象并声明计算机随机变量
作为一个实际的例子,我在main方法中创建了一个游戏循环(通常有无穷多个:),在这里我接收用户输入和计算机输入,然后进行比较。成功伴随着祝贺,失败伴随着重试。我并不认为这正是你想要得到的,但它可能是
这是我完整的说明代码
import java.util.Scanner;
import java.util.Random;
/**
* Created by alompo on 07.11.16.
*/
public class RockPaperScissors {
static String userChoice = "";
static String cpuChoice = "";
static Scanner in = new Scanner(System.in);
static Random r = new Random();
public static void main(String[] args) {
while(true) {
getUserChoice();
getCPUChoice();
if (cpuChoice.equalsIgnoreCase(userChoice)) {
System.out.println("Good choice, you have a match with the computer");
} else {
System.out.println("Sorry mismatch between you and the computer...try again");
}
}
}
public static void getUserChoice()
{
System.out.println("Would you you like to play rock, paper scissors?");
userChoice = in.nextLine();
while (!userChoice.equalsIgnoreCase("Rock") && !userChoice.equalsIgnoreCase("Paper") && !userChoice.equalsIgnoreCase("Scissors"))
{
System.out.println("Invalid entry please try again");
userChoice = in.nextLine();
}
} //end
public static void getCPUChoice()
{
int computerRandom = r.nextInt(3)+1;
if (computerRandom == 1) cpuChoice = "Rock";
else if (computerRandom == 2) cpuChoice = "Paper";
else if (computerRandom == 3) cpuChoice = "Scissors";
}
}
你不能把类放在函数中。那么更像是公共静态吗?人们常说类代表名词,方法代表动词;类是一件事,而方法是一件事
getUserChoice
显然是要做的事情,所以它应该是一个方法,而不是一个类。在编写自己的程序之前,您可能需要从一些非常基本的教程开始理解这些概念。@Itamagreen-当然可以!我只是自己重复一遍,没有要求任何荣誉。@TylerK-现在你添加了类pickWinner
,这应该是一个方法而不是一个类。我认为问题是他们对类结构没有基本的理解。我认为内部/匿名类目前可能有点超出他的理解范围,可能更好的方法是将其作为带有方法的单个类来显示。因此,如果我执行类似于just class的操作,它将绕过该错误,但仍然会得到预期的标识符。尝试只执行class方法,但它仍然无法编译。我还有更大的问题吗?它返回大量错误,这些错误在我以前做过的程序中是不存在的。我在我的帖子中更新了我的原始代码以展示我所拥有的。因为类声明不是方法。。。它们不返回类型。
import java.util.Scanner;
import java.util.Random;
/**
* Created by alompo on 07.11.16.
*/
public class RockPaperScissors {
static String userChoice = "";
static String cpuChoice = "";
static Scanner in = new Scanner(System.in);
static Random r = new Random();
public static void main(String[] args) {
while(true) {
getUserChoice();
getCPUChoice();
if (cpuChoice.equalsIgnoreCase(userChoice)) {
System.out.println("Good choice, you have a match with the computer");
} else {
System.out.println("Sorry mismatch between you and the computer...try again");
}
}
}
public static void getUserChoice()
{
System.out.println("Would you you like to play rock, paper scissors?");
userChoice = in.nextLine();
while (!userChoice.equalsIgnoreCase("Rock") && !userChoice.equalsIgnoreCase("Paper") && !userChoice.equalsIgnoreCase("Scissors"))
{
System.out.println("Invalid entry please try again");
userChoice = in.nextLine();
}
} //end
public static void getCPUChoice()
{
int computerRandom = r.nextInt(3)+1;
if (computerRandom == 1) cpuChoice = "Rock";
else if (computerRandom == 2) cpuChoice = "Paper";
else if (computerRandom == 3) cpuChoice = "Scissors";
}
}