- Jul 27, 2001
- 4,595
- 1
- 81
If you know a lot about Java and have a little time to spare, let me know if you can help. This involves figuring an array. Thanks in advance. Run the program in textpad and see the results...I'm trying to get each players points to add together for a Grand Total in Points.
/* This program runs a candy game involving colors with point value system. Each player enters
his/her name then the number of each color candy he/she has. The color point values are brown=1,
yellow=4, green=8, red=11, orange=7 and blue=15. The winner is the one with the most points.
*/
import java.awt.*;
import javax.swing.*;
public class candyprogram
{
public static void main( String args[] )
{
//declaring integers
int brown,yellow,green,red,orange,blue,
playerTotal=0, i=0, candyTotal=0, points=0, colorTotal=0;
String strBrown, strYellow, strGreen, strRed, strOrange, strBlue,
TotalTemp, name = "",output = "";
TotalTemp = JOptionPane.showInputDialog(
"How many players are in the game?");
playerTotal = Integer.parseInt(TotalTemp);
int candy[][];
candy = new int
[playerTotal+1][7];
String names[];
names = new String [playerTotal];
output = "\tBrown\tYellow\tGreen\tRed\tOrange\tBlue\tPoints\n";
while (i < playerTotal )
{
//Get number of each color from player
name = JOptionPane.showInputDialog(
"Enter name of player");
names = name;
output = output + names + "\t";
strBrown = JOptionPane.showInputDialog("How many brown candies are there?");
brown = Integer.parseInt( strBrown );
candyTotal += brown;
candy = brown;
points = points + brown;
output = output + candy +"\t";
strYellow = JOptionPane.showInputDialog("How many yellow candies are there?");
yellow = Integer.parseInt( strYellow );
candyTotal += yellow;
candy[i+1] = yellow;
points = points + yellow * 4;
output = output + candy[i+1] + "\t";
strGreen = JOptionPane.showInputDialog("How many green candies are there?");
green = Integer.parseInt( strGreen );
candyTotal += green;
candy[i+5] = green;
points = points + green * 8;
output = output + candy[i+2] +"\t";
strRed = JOptionPane.showInputDialog("How many red candies are there?");
red = Integer.parseInt( strRed );
candyTotal += red;
candy[i+3] = red;
points = points + red * 11;
output = output + candy[i+3] +"\t";
strOrange = JOptionPane.showInputDialog("How many orange candies are there?");
orange = Integer.parseInt( strOrange );
candyTotal += orange;
candy[i+2] = orange;
points = points + orange * 7;
output = output + candy[i+4] +"\t";
strBlue = JOptionPane.showInputDialog("How many blue candies are there?");
blue = Integer.parseInt( strBlue );
candyTotal += blue;
candy[i+4] = blue;
points = points + blue * 15;
output = output + candy[i+5] +"\t" + points + "\n";
JOptionPane.showMessageDialog (null, "Candy results for " + name + "\n"
+ "Total points = " + points + "\n"
+ "Total Candies = " + candyTotal,"Candy results",
JOptionPane.INFORMATION_MESSAGE);
i++;
//ends while loop
}
JTextArea outputArea = new JTextArea ();
outputArea.setText (output);
JOptionPane.showMessageDialog (null, outputArea, "Candy Game Results",
JOptionPane.INFORMATION_MESSAGE );
System.exit ( 0 );
}
}
/* This program runs a candy game involving colors with point value system. Each player enters
his/her name then the number of each color candy he/she has. The color point values are brown=1,
yellow=4, green=8, red=11, orange=7 and blue=15. The winner is the one with the most points.
*/
import java.awt.*;
import javax.swing.*;
public class candyprogram
{
public static void main( String args[] )
{
//declaring integers
int brown,yellow,green,red,orange,blue,
playerTotal=0, i=0, candyTotal=0, points=0, colorTotal=0;
String strBrown, strYellow, strGreen, strRed, strOrange, strBlue,
TotalTemp, name = "",output = "";
TotalTemp = JOptionPane.showInputDialog(
"How many players are in the game?");
playerTotal = Integer.parseInt(TotalTemp);
int candy[][];
candy = new int
[playerTotal+1][7];
String names[];
names = new String [playerTotal];
output = "\tBrown\tYellow\tGreen\tRed\tOrange\tBlue\tPoints\n";
while (i < playerTotal )
{
//Get number of each color from player
name = JOptionPane.showInputDialog(
"Enter name of player");
names = name;
output = output + names + "\t";
strBrown = JOptionPane.showInputDialog("How many brown candies are there?");
brown = Integer.parseInt( strBrown );
candyTotal += brown;
candy = brown;
points = points + brown;
output = output + candy +"\t";
strYellow = JOptionPane.showInputDialog("How many yellow candies are there?");
yellow = Integer.parseInt( strYellow );
candyTotal += yellow;
candy[i+1] = yellow;
points = points + yellow * 4;
output = output + candy[i+1] + "\t";
strGreen = JOptionPane.showInputDialog("How many green candies are there?");
green = Integer.parseInt( strGreen );
candyTotal += green;
candy[i+5] = green;
points = points + green * 8;
output = output + candy[i+2] +"\t";
strRed = JOptionPane.showInputDialog("How many red candies are there?");
red = Integer.parseInt( strRed );
candyTotal += red;
candy[i+3] = red;
points = points + red * 11;
output = output + candy[i+3] +"\t";
strOrange = JOptionPane.showInputDialog("How many orange candies are there?");
orange = Integer.parseInt( strOrange );
candyTotal += orange;
candy[i+2] = orange;
points = points + orange * 7;
output = output + candy[i+4] +"\t";
strBlue = JOptionPane.showInputDialog("How many blue candies are there?");
blue = Integer.parseInt( strBlue );
candyTotal += blue;
candy[i+4] = blue;
points = points + blue * 15;
output = output + candy[i+5] +"\t" + points + "\n";
JOptionPane.showMessageDialog (null, "Candy results for " + name + "\n"
+ "Total points = " + points + "\n"
+ "Total Candies = " + candyTotal,"Candy results",
JOptionPane.INFORMATION_MESSAGE);
i++;
//ends while loop
}
JTextArea outputArea = new JTextArea ();
outputArea.setText (output);
JOptionPane.showMessageDialog (null, outputArea, "Candy Game Results",
JOptionPane.INFORMATION_MESSAGE );
System.exit ( 0 );
}
}