80分求救 java问题 要分的 就进来!!

2024年12月02日 19:56
有4个网友回答
网友(1):

text.pan(num1,num2);
text.showgame(count, com, people, name, computer);
兄弟你这是调用另外一个类中的参数啊,能不出错吗?另外一个类中的参数要用get方法得到,或者设置为静态属性,或者通过返回值来获取
按照你的思路给你改出来了
package com.business;

import java.util.*;
public class Game {
private String name;
private String computer="曹操";
private int num;
private int num1;
private int num2;
private String answer;
private int count=0;
private int people=0;
private int com=0;
Scanner input=new Scanner(System.in);
public void person(){
System.out.println("请选择玩家姓名");
System.out.println("1.张飞 2.关羽");
num=input.nextInt();
switch(num){
case 1:
this.name="张飞";
break;
case 2:
this.name="关羽";
break;
}
}
public void startgame(){

System.out.println("请出拳 1.剪刀 2.石头 3.布");
num1=input.nextInt();
switch(num1){
case 1:
System.out.println("你出的是 剪刀");
break;
case 2:
System.out.println("你出的是 石头");
break;
case 3:
System.out.println("你出的是 布");
break;
}
num2=(int)(Math.random()*10%3+1);//电脑出拳
if(num2==1){
System.out.println("电脑出的是剪刀");
}
else if(num2==2){
System.out.println("电脑出的是石头");
}
else if(num2==3){
System.out.println("电脑出的是布");
}
}
public void pan(int num1,int num2){
do{
if(( num1==1)&&( num2==3)){
System.out.println("玩家胜利");
people++;
}
else if((num1==2)&&(num2==1)){
System.out.println("玩家胜利");
people++;
}
else if((num1==3)&&(num2==2)){
System.out.println("玩家胜利");
people++;
}
else if((num1==1)&&(num2==2)){
System.out.println("电脑胜利");
com++;
}
else if((num1==2)&&(num2==3)){
System.out.println("电脑胜利");
com++;
}
else if((num1==3)&&(num2==1)){
System.out.println("电脑胜利");
com++;
}
count++; //总的次数
System.out.println("是否继续 y?");

answer=input.next();
}while(!answer.equals("y"));
}
public void showgame(int count,int com,int people,String name,String computer){
System.out.println(name+"VS"+computer);
System.out.println("总局数"+count);
System.out.println("玩家胜利的次数"+people);
System.out.println("电脑胜利的次数"+com);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getComputer() {
return computer;
}
public void setComputer(String computer) {
this.computer = computer;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getNum1() {
return num1;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
this.num2 = num2;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getPeople() {
return people;
}
public void setPeople(int people) {
this.people = people;
}
public int getCom() {
return com;
}
public void setCom(int com) {
this.com = com;
}
}

package com.business;

public class Start {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Game text=new Game();
text.person();
text.startgame();
text.pan(text.getNum1(),text.getNum2());
text.showgame(text.getCount(), text.getCom(), text.getPeople(), text.getName(), text.getComputer());
}
}
经过测试通过,有什么不明白的可以发消息!

网友(2):

import 包名.game; //首先导包

public class start {

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
game text=new game();
text.person();
text.startgame();
text.pan(num1,num2);
text.showgame(count, com, people, name, computer);

}
}

然后把你这个类里面的方法都改成静态的,方法名前面加static
import java.util.*;
public class game {
Scanner input=new Scanner(System.in);
public static void person(){

String name;
String computer="曹操";
System.out.println("请选择玩家姓名");
System.out.println("1.张飞 2.关羽");
int num=input.nextInt();
switch(num){
case 1:
name="张飞";
break;
case 2:
name="关羽";
break;
}
}
public static void startgame(){

System.out.println("请出拳 1.剪刀 2.石头 3.布");
int num1=input.nextInt();
switch(num1){
case 1:
System.out.println("你出的是 剪刀");
break;
case 2:
System.out.println("你出的是 石头");
break;
case 3:
System.out.println("你出的是 布");
break;
}
int num2=(int)(Math.random()*10%3+1);//电脑出拳
if(num2==1){
System.out.println("电脑出的是剪刀");
}
else if(num2==2){
System.out.println("电脑出的是石头");
}
else if(num2==3){
System.out.println("电脑出的是布");
}
}
public static void pan(int num1,int num2){
String answer;
int count=0;
int people=0;
int com=0;
do{
if(( num1==1)&&( num2==3)){
System.out.println("玩家胜利");
people++;
}
else if((num1==2)&&(num2==1)){
System.out.println("玩家胜利");
people++;
}
else if((num1==3)&&(num2==2)){
System.out.println("玩家胜利");
people++;
}
else if((num1==1)&&(num2==2)){
System.out.println("电脑胜利");
com++;
}
else if((num1==2)&&(num2==3)){
System.out.println("电脑胜利");
com++;
}
else if((num1==3)&&(num2==1)){
System.out.println("电脑胜利");
com++;
}
count++; //总的次数
System.out.println("是否继续 y?");

answer=input.next();
}while(!answer.equals("y"));
}
public static void showgame(int count,int com,int people,String name,String computer){
System.out.println(name+"VS"+computer);
System.out.println("总局数"+count);
System.out.println("玩家胜利的次数"+people);
System.out.println("电脑胜利的次数"+com);
}
}

介意你用get方法

网友(3):

num1,num2,count,com,people,name,computer都没有定义当然所错.

改的话,太多了.以下是我的代码,可能有些面目全非了.
/*
* Game.java
*/
import java.util.*;

public class Game {
private int personNum;
private String personName;
private int computerNum;
private Scanner input = new Scanner(System.in);
private int count = 0;
private int people = 0;
private int com = 0;
public void person() {
System.out.println("请选择玩家姓名");
System.out.println("1.张飞 2.关羽");
int num = input.nextInt();
switch (num) {
case 1:
personName = "张飞";
break;
case 2:
personName = "关羽";
break;
}
}

public void startGame() {
String answer;
do {
System.out.println("请出拳 1.剪刀 2.石头 3.布");
int num1 = input.nextInt();
personNum = num1;
switch (num1) {
case 1:
System.out.println("你出的是 剪刀");
break;
case 2:
System.out.println("你出的是 石头");
break;
case 3:
System.out.println("你出的是 布");
break;
}
int num2 = (int) (Math.random() * 3 + 1);// 电脑出拳
computerNum = num2;
if (num2 == 1) {
System.out.println("电脑出的是剪刀");
} else if (num2 == 2) {
System.out.println("电脑出的是石头");
} else if (num2 == 3) {
System.out.println("电脑出的是布");
}
pan(personNum, computerNum);
showGame();
System.out.println("是否继续 y?");
answer = input.next();
} while (answer.equals("y"));
}

private void pan(int num1, int num2) {
if ((num1 == 1) && (num2 == 3)) {
System.out.println("玩家胜利");
people++;
} else if ((num1 == 2) && (num2 == 1)) {
System.out.println("玩家胜利");
people++;
} else if ((num1 == 3) && (num2 == 2)) {
System.out.println("玩家胜利");
people++;
} else if ((num1 == 1) && (num2 == 2)) {
System.out.println("电脑胜利");
com++;
} else if ((num1 == 2) && (num2 == 3)) {
System.out.println("电脑胜利");
com++;
} else if ((num1 == 3) && (num2 == 1)) {
System.out.println("电脑胜利");
com++;
}
count++; // 总的次数
}

private void showGame() {
System.out.println(personName + "VS" + "computer");
System.out.println("总局数" + count);
System.out.println("玩家胜利的次数" + people);
System.out.println("电脑胜利的次数" + com);
}
}
/*
*Start.java
*/
public class Start {

public static void main(String[] args) {
Game text = new Game();
text.person();
text.startGame();
}
}

网友(4):

从屏幕读入据怎么能放到各个方法体里去了呢?你把那些方法都加上参数,然后以读入的数作参数调用那些方法就行了