Topic: 求救:小程序编不下去了,请帮帮忙啊

  Print this page

1.求救:小程序编不下去了,请帮帮忙啊 Copy to clipboard
Posted by: haitao517
Posted on: 2008-10-14 03:42

小弟是刚开始学习Java的,对Java有浓厚的兴趣,近来无事,想用它来编一个UNO(一种扑克牌玩法) 的计分器。要求结果打印出一个表格来,上面是玩家的名字,然后新的一行是局数,然后玩家名字下对应每个玩家的该局得分,第二局的得分是该局得分加上上一局的得分。直到某个玩家的总分达到500分,游戏结束。例:
Player1 Player2 Player3
Round1 10 20 0
Round2 100 300 0
Round3 210 500 0

Player2 is loser !!!

小弟只编了一个开始,现在不知道如何把第一局的分数保存,然后跟第二局的分数相加,没有思路了

请各位大侠帮帮忙,给点意见啊,小弟附上2个自己写的java程序,没有完工的,大侠们看了不要拍砖啊(新手写的)

Desktop.rar (0.69k)

2.Re:求救:小程序编不下去了,请帮帮忙啊 [Re: haitao517] Copy to clipboard
Posted by: skwujinhua
Posted on: 2008-10-14 09:21

試試static 變量保存你的積分。

3.Re:求救:小程序编不下去了,请帮帮忙啊 [Re: haitao517] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-10-14 16:55

The question is very well structured, very easy to understand. In addition you provided the source code, I'm really impressed.

To answer your previous question, I think either using *variable* or *static variable* will meet your requirement here. see my following example:
(although I don't see any reason here to use *static variable*)

===============================================
The following solution is based on modifying the Player class

The simplest solution I can think of is that you could declare another variable, namely "latestScore" in the Player.class, to store the latest score of the game. It must have getter/setter methods for its client to update the latestScore. (the client here is simply your UnoPlay class). However this solution might *not* look very Object-oriented, as it duplicated the variable aPoint IMHO. Hence I don't suggest this approach, although it is very simple.

===============================================
The following solutions are based on modifying the client (i.e. UnoPlay class)


// declare instance variables
private short accScore;

....
// whenever there is a change in the new score
// assume that the newScore is the variable you store the new score.
accScore += newScore;


You will probably declare these accScore in a Map as you have multiple players in the game, each map will key on a player's name (however you need to make sure that the names of the players are unique) i.e.

// declare instance variable
// where key is the player's name and value is the accumulated score.
private Map<String, Short > accScoresMap = new HashMap<String, Short>();

// whenever there is a change specific to a person
// assuming that playerName = the player's name
// currentScore = the score of the current play.
// 1. check if the map contains such a person
if (accScoresMap.contains(playerName))
{
// 2. get the score and update the map
Short score = accScoreMap.get(playerName);
accScoreMap.put(playerName, score+=currentScore);
}
else
{
// exception handing...
}


Another solution is that you can use the Player class an the key of the Map, however in that case, you need to implement the hashCode() and equal() method in your Player class. I leave this as an exercise for you to complete.

NOTE: The pros/cons of Java programming language is its support for the OOD (Object Oriented Design). It is really a double-edged sword. In other words, using it inappropriately will make the code even worse than not using OOD. In general, we should always think about encapsulation, inheritance and polymorphism before implementing any code in Java. e.g. Have all the code done in the main() method is obviously not a very good object-oriented design and it should be avoided. You are probably reverted good Java code back to those old procedural programming language like C.
Jiafan

4.Re:求救:小程序编不下去了,请帮帮忙啊 [Re: haitao517] Copy to clipboard
Posted by: haitao517
Posted on: 2008-10-14 23:59

谢谢Jiafan 的回答,引用你的这句话 "Have all the code done in the main() method is obviously not a very good object-oriented design and it should be avoided." 我会记住的.
最近我在读 "Java核心编程",还有 "Java数据结构和算法", 从中学到了Java的很多东西,但是我感觉自己对Java语法结构还是很欠缺的,我希望能在这里跟各位大侠们交流学习,来提高自己。
如果提出什么白痴的,弱智的问题的话,大家不要拍我啊。

JiaFan,你给我的建议,我会继续研究的,再次的谢谢你

5.Re:求救:小程序编不下去了,请帮帮忙啊 [Re: haitao517] Copy to clipboard
Posted by: haitao517
Posted on: 2008-10-15 02:24

还有个问题想请教一下 JiaFan,能给我推荐本书吗?是关于Java语法结构的,能让我写出来的程序看上去是面向对象的,而不是像在学校里所学的那些结构化的写法。

6.Re:求救:小程序编不下去了,请帮帮忙啊 [Re: haitao517] Copy to clipboard
Posted by: haitao517
Posted on: 2008-10-15 04:05

From haitao517 :
"发个改过的程序,还请JiaFan指点一下,谢谢"

From Jiafan:
"I will take some time to review your code soon, but I cannot grantee you when I will complete. Hopefully by the end of this weekend. Smile"

Uno.rar (1.07k)

7.Re:求救:小程序编不下去了,请帮帮忙啊 [Re: haitao517] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-10-15 18:36

haitao517 wrote:
还有个问题想请教一下 JiaFan,能给我推荐本书吗?是关于Java语法结构的,能让我写出来的程序看上去是面向对象的,而不是像在学校里所学的那些结构化的写法。

<<Thinking in Java 4th Edition>>. And I also encourage that you should read this book in its original English Edition version.

Maybe you will find this book a little bit intimidating to understand first, but don't worry. This book was written and intended for Intermediate and advanced Java programmers, however on your ladder to the success of learning Java, you will read this book at some stage. In addition, this book deserves some place on your bookshelf. Smile

This book is also called the "Bible" for all the Java developers. It includes all the nitty-gritty of the Java Basics.

8.Re:求救:小程序编不下去了,请帮帮忙啊 [Re: haitao517] Copy to clipboard
Posted by: andy_wang_5
Posted on: 2008-10-16 09:55

同意, 我以前读的是 3th Edition 中文版.
现在我正在看4th Edition 英文版.
1. 这本书正的很不错
2. 可以学习英语, 做我们这行英语不行. 就像男人阳萎一样.....

9.Re:求救:小程序编不下去了,请帮帮忙啊 [Re: haitao517] Copy to clipboard
Posted by: haitao517
Posted on: 2008-10-16 23:04

哎,同意楼上的啊,不过小弟我现在在德国,把德语学了,英语就不行了,现在要恶补英语啊,还是英语重要!!!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923