大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
300行能干啥啊,你还不如自己做做练习题。
成都创新互联公司主要从事成都网站建设、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务五寨,十年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575
package suoha;
import java.util.ArrayList;import java.util.Random;
public class MainFunction {
private ArrayListMyCard cards = new ArrayListMyCard();
private int[] cardNumArr = new int[52];
private int[] mycards = new int[5];
private int[] p1cards = new int[5];
public static void main(String[] args) {
MainFunction mainfunc = new MainFunction();
mainfunc.makeCards();
mainfunc.cardNumArr = mainfunc.shuffleCards(mainfunc.cardNumArr);
mainfunc.catchCards();
mainfunc.showAllCardsOfHand();
//mainfunc.printNums();
}
/**
* 洗牌
* @param cardNums
* @return
*/
private int[] shuffleCards(int[] cardNums){
int size = cardNums.length;
int[] returnNums = new int[size];
//System.out.println(cardNums[0]+"=========================");
Random rand = new Random();
for(int i=size-1;i0;i--){
int nextInt = rand.nextInt(i);
returnNums[size-1-i] = cardNums[nextInt];
cardNums[nextInt] = cardNums[i];
cardNums[i] = returnNums[size-1-i];
//System.out.println("suijishu:"+nextInt+"||zhi:"+returnNums[size-1-i]);
}
returnNums[size-1] = cardNums[0];
return returnNums;
}
/**
* 抓牌
*/
private void catchCards(){
for(int i=0;i5;i++){
mycards[i] = cardNumArr[2*i];
p1cards[i] = cardNumArr[2*i+1];
}
}
private void showAllCardsOfHand(){
System.out.print("你手中的牌是:||");
for(int i=0;i5;i++){
MyCard myCard = cards.get(mycards[i]-1);
System.out.print(myCard.toString()+"||");
}
System.out.println();
System.out.print("对家手中的牌是:||");
for(int i=0;i5;i++){
MyCard myCard = cards.get(p1cards[i]-1);
System.out.print(myCard.toString()+"||");
}
}
/**
* 显示牌的顺序
*/
private void printNums(){
for(int i=0;icardNumArr.length;i++){
System.out.print(cardNumArr[i]+",");
}
}
/**
* 制作卡片
*/
private void makeCards(){
for(int i=1;i=13;i++){
for(int j=1;j=4;j++){
//System.out.println((i-1)*4+j);
MyCard card = new MyCard((i-1)*4+j, j, i);
cardNumArr[(i-1)*4+j-1] = (i-1)*4+j;
cards.add(card);
}
// MyCard card1 = new MyCard((i-1)*4+1, 1, i);
// MyCard card2 = new MyCard((i-1)*4+2, 2, i);
// MyCard card3 = new MyCard((i-1)*4+3, 3, i);
// MyCard card4 = new MyCard((i-1)*4+4, 4, i);
// cards.add(card1);
// cards.add(card2);
// cards.add(card3);
// cards.add(card4);
}
}
}
1.首先创建Player类(即Player.java文件),代码如下
public class Player {
private String lastName;
private String firstName;
private int pointsScored;
private int assists;
private double penaltyKickRate;
public Player(){
}
public Player(String lastName, String firstName, int pointsScored, int assists, double penaltyKickRate){
this.lastName = lastName;
this.firstName = firstName;
this.pointsScored = pointsScored;
this.assists = assists;
this.penaltyKickRate = penaltyKickRate;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getPointsScored() {
return pointsScored;
}
public void setPointsScored(int pointsScored) {
this.pointsScored = pointsScored;
}
public int getAssists() {
return assists;
}
public void setAssists(int assists) {
this.assists = assists;
}
public double getPenaltyKickRate() {
return penaltyKickRate;
}
public void setPenaltyKickRate(double penaltyKickRate) {
this.penaltyKickRate = penaltyKickRate;
}
}
2.创建测试类Main(即Main.java文件)代码如下:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
static ListPlayer players = new ArrayListPlayer();
static Scanner scan = new Scanner(System.in);
public static void add(){
String lastName = "";
String firstName = "";
int pointsScored = 0;
int assists = 0;
double penaltyKickRate = 0;
System.out.println("请输入球员的姓:");
lastName = scan.next();
System.out.println("请输入球员的名:");
firstName = scan.next();
System.out.println("请输入该球员点球得分:");
pointsScored = scan.nextInt();
System.out.println("请输入该球员助攻得分:");
assists = scan.nextInt();
System.out.println("请输入该球员罚球得分概率");
penaltyKickRate = scan.nextDouble();
System.out.print("** PLAYER ADDED ** ");
Player player = new Player(lastName, firstName, pointsScored, assists, penaltyKickRate);
players.add(player);
System.out.println(lastName + firstName + ": Points:" + pointsScored
+ " Assists: " + assists + " Penalty Kick %: " + penaltyKickRate);
}
public static void remove(){
boolean isRemoved = false;
String lastName = "";
String firstName = "";
System.out.println("请输入要删除的球员姓:");
lastName = scan.next();
System.out.println("请输入要删除的球员名:");
firstName = scan.next();
for (int i = 0; i players.size(); i++) {
Player player = players.get(i);
if (player.getLastName().equals(lastName) player.getFirstName().equals(firstName)) {
players.remove(i);
System.out.println("** REMOVED " + lastName + firstName + " **");
isRemoved = true;
}
}
if (!isRemoved) {
System.out.println("** 队中没有您输入的队员姓名 **");
}
}
public static void show(){
if (players.isEmpty()) {
System.out.println("** 您还没有添加队员! **");
}else {
System.out.println("** SHOW ALL PLAYERS **");
for (Player player : players) {
System.out.println(player.getLastName() + player.getFirstName() + ": Points: " + player.getPointsScored()
+ " Assists: " + player.getAssists() + " Penalty Kick %: " + player.getPenaltyKickRate() + " **");
}
}
}
public static void save() throws FileNotFoundException{
System.out.println("请输入保存文件名:");
String fileName = scan.next();
PrintWriter out = new PrintWriter(new File(fileName));
for (Player player : players) {
out.print(player.getLastName() + player.getFirstName() + ": Points: " + player.getPointsScored()
+ " Assists: " + player.getAssists() + " Penalty Kick %: " + player.getPenaltyKickRate() + " **");
out.print("\n");
}
out.flush();
System.out.println(" ** " + players.size() + " RECORD SAVED TO " + fileName + " **");
}
public static void main(String[] args) throws FileNotFoundException {
String operate = "";
while(true){
System.out.println("下面如何操作(ADD, REMOVE, SHOW, SAVE, QUIT)?");
operate = scan.next();
if ("ADD".equalsIgnoreCase(operate)) {
add();
}else if ("REMOVE".equalsIgnoreCase(operate)) {
remove();
}else if ("SHOW".equalsIgnoreCase(operate)) {
show();
}else if ("SAVE".equalsIgnoreCase(operate)) {
save();
}else if ("QUIT".equalsIgnoreCase(operate)) {
break;
}else {
System.out.println("该输入不合法,请输入合法值");
}
}
System.out.println("** GOODBYE! **");
}
}
3.注意如果输入的姓名要输入汉字的话一定要让光标移到下一行(在eclipse中即单击一下输入界面)否则可能出现无法输入的情况,如果都是用英文就不会有这样的情况
4.测试结果如图
简单,你要是 不想下eclipse,就进入bin目录运行java TestCardType看能否运行(看名字估计main方法在那里面)。
如果你想研究代码,用eclipse,菜单上的new--project...选project from existing source,然后选择这个程序目录,就导入了,然后点eclipse上的那个运行按钮就行了。
答案是D。
1.类的字段成员会初始化为默认值
2.在静态方法main中不能访问Test类里的s,因为它不是static的
3.在外围类的方法main中可以创建static内部类Inner的对象
首先你要在你的电脑上安装jdk。你可以在后面链接地址下载适合你自己的版本(),如果这个链接过期了,请在这个首先找一找。
在你的电脑上配置java环境变量,主要是配置path和classpath。你可以百度java环境变量配置,可以找到很多java环境变量配置方法。配置完毕,可以在cmd窗口下用java -version来查看是否配置成功。如果显示出java版本相关的信息表示配置成功,可以进行下一步了。
编译你的源代码,cmd窗口下把路径改变(cd)到你源代码文件所在的路径,然后用javac 源文件名编译,例如javac Hello.java(需要注意的是源文件名需要是你文件public类的类名,如果你的文件有public类的话)。当然你也可以不改变(cd)到源文件所在的路径,你的文件就需要加上绝对路径就可以了。例如:javac e:\src\Hello.java.
运行你编译好的文件,java Hello(需要注意运行的时候没有后缀.java或者.class),同样你可以不改变路径用绝对路径运行,例如:java e:\src\Hello.如果你的代码中有窗口这样的类似的图形化界面,你就需要用javaw来运行。
另外,你可以使用eclipse,NetBeans这样的集成开发环境(IDE)来写代码,这样方便很多。