// The "FriedmanRPSgame" class.
import java.awt.*;
import hsa.Console;
public class FriedmanRPSgame
{
static Console c; // The output console
public static void main (String[] args)
{
c = new Console ();
// VARIABLES
int rock;
int paper;
int scissors;
// INPUT
c.println (" Choose 1 for rock, 2 for paper, 3 for scissors");
int choice =c.readInt();
int computer = Math.floor((Math.random()*3)+1);
if (Computer == 1 && choice == 2 ||Computer == 2 && choice == 3 || computer == 3 && choice == 1)
{
c.println ("You win!!");
}
else if (choice == 1 && computer == 2 || choice == 2 && computer == 3 || choice == 3 && computer == 1)
{
c.println ("You Lose!!");
}
else if (computer == choice)
{
c.println ("Tryagain!");
}
// Place your program here. 'c' is the output console
} // main method
} // FriedmanRPSgame class
Taywin 312 Posting Virtuoso
And what is your question?
sirlink99 56 Practically a Master Poster
first of all you have capitalization problems in your code (Computer). Then you also need to put brackets around each of the cases when you are checking them.
Edited by sirlink99
tux4life 2,072 Postaholic
Compiler output:
FriedmanRPSgame.java:3: error: package hsa does not exist
import hsa.Console;
^
FriedmanRPSgame.java:7: error: cannot find symbol
static Console c; // The output console
^
symbol: class Console
location: class FriedmanRPSgame
FriedmanRPSgame.java:11: error: cannot find symbol
c = new Console ();
^
symbol: class Console
location: class FriedmanRPSgame
FriedmanRPSgame.java:21: error: possible loss of precision
int computer = Math.floor((Math.random()*3)+1);
^
required: int
found: double
FriedmanRPSgame.java:24: error: cannot find symbol
if (Computer == 1 && choice == 2 ||Computer == 2 && choice == 3 || computer ==
3 && choice == 1)
^
symbol: variable Computer
location: class FriedmanRPSgame
FriedmanRPSgame.java:24: error: cannot find symbol
if (Computer == 1 && choice == 2 ||Computer == 2 && choice == 3 || computer ==
3 && choice == 1)
^
symbol: variable Computer
location: class FriedmanRPSgame
6 errors
In addition to what has been said:
- Math.floor() returns a double, and you are assigning it to a primitive of type
int
, what you want is a cast. hsa.Console
? From the context I assume that this class is used for outputting things. I'm just wondering... What is the great benefit of using this class as opposed to using something that comes with the Java libraries? I suggest you to take a look here. You can access an instance of PrintStream using the staticout
reference variable of the System class. E.g.System.out.println("You win!!!")
.- Next time it would be great if you could point out the issue that you are encountering, How To Ask Questions The Smart Way by ESR is a wonderful read.
Edited by tux4life
sirlink99 56 Practically a Master Poster
hsa.Console? From the context I assume that this class is used for outputting things. I'm just wondering... What is the great benefit of using this class as opposed to using something that comes with the Java libraries? I suggest you to take a look here. You can access an instance of PrintStream using the static out reference variable of the System class. E.g. System.out.println("You win!!!").
I recongize the hsa console. It is a built in console in the Ready to Program IDE. It is essentially a TextArea that works like the command prompt and "simplifies" reading user input that is entered into the console.
tux4life commented: Oh, okay, I didn't know that. +13
Zirely 16 Newbie Poster
Hey there, I am new programmer and just found about this I am trying to find out what I could use from the Java libraries to replace hsa.Console?
rproffitt commented: This thread is old. Make a new discussion to find the gold. +16
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
AFAIK there isn't anything, which is why I hacked this together a while back.
Feel free to use it without any warrenty.
SImply call ConsoleWindow.withInput();
and continue to use System.out (or err) as usual. Read from System.in as usual, eg via a Scanner
.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import static java.awt.GridBagConstraints.*;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
/**
* Creates a window with a scrolling text pane diverts all subsequent System.out
* and System.err to it allows system input via text field and enter button
* "cls\n" sent to System,out or err will clear the window
*
* Default action when closed is to exit the whole app, but can be overridden
*
* eg: ConsoleWindow.withInput(); // now System.in/out/err go here
*
* @author James
*/
public class ConsoleWindow extends JFrame {
// factory methods for window with/without input field...
public static ConsoleWindow displayOnly() {
return new ConsoleWindow(false);
}
public static ConsoleWindow withInput() {
return new ConsoleWindow(true);
}
// (optional) override default window close action (exit program)
public ConsoleWindow setClose(Runnable r) {
// Runnable to execute when this window is closed.
closeHandler = r;
return this;
}
// end of public members. Private implementation follows...
private final String CLEAR_SCREEN = "cls\n";
private final JTextPane textPane = new JTextPane();
private final JTextField inputField = new JTextField(30);
private final JButton enterButton = new JButton("Enter");
private final Font font = new Font("Monospaced", Font.PLAIN, 16);
private PrintStream toSystemIn; // pipes to System.in (optional)
private final PrintStream inputEcho = new PrintStream(new TextAreaOutputStream(Color.blue));
private Runnable closeHandler = () -> System.exit(0); // default, see setClose method
private ConsoleWindow(boolean allowInput) {
setTitle("System.out & System.err");
setLayout(new GridBagLayout());
textPane.setFont(font);
add(new JScrollPane(textPane), new GridBagConstraints(0, 0, 2, 1, 1, 1, CENTER, BOTH,
new Insets(3, 3, 3, 03), 0, 0));
if (allowInput) {
addSysin();
}
pack();
setSize(new Dimension(300, 140));
setLocationRelativeTo(null);
setVisible(true);
getRootPane().setDefaultButton(enterButton);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
closeHandler.run();
}
});
System.setOut(new PrintStream(new TextAreaOutputStream(Color.black)));
System.setErr(new PrintStream(new TextAreaOutputStream(Color.red)));
}
private void addSysin() {
inputField.setFont(font);
add(inputField, new GridBagConstraints(0, 1, 1, 1, 1, 0, EAST, HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
add(enterButton, new GridBagConstraints(1, 1, 1, 1, 0, 0, WEST, HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0));
enterButton.addActionListener(this::enterPressed);
var pipeIn = new PipedInputStream(1024);
var pipeOut = new PipedOutputStream();
try {
pipeOut.connect(pipeIn);
} catch (IOException e) { // will never happen
e.printStackTrace();
System.exit(-1);
}
toSystemIn = new PrintStream(pipeOut);
System.setIn(pipeIn);
pack();
inputField.requestFocusInWindow();
}
private void enterPressed(ActionEvent e) {
String s = inputField.getText();
inputField.setText("");
inputEcho.println(s);
toSystemIn.println(s);
}
private class TextAreaOutputStream extends OutputStream {
private final AttributeSet styleAttributes;
public TextAreaOutputStream(Color color) {
StyleContext sc = StyleContext.getDefaultStyleContext();
styleAttributes = sc.addAttribute(SimpleAttributeSet.EMPTY,
StyleConstants.Foreground, color);
}
private StringBuilder inputHistory = new StringBuilder();
@Override
public void write(int b) throws IOException {
textPane.setCaretPosition(textPane.getDocument().getLength());
textPane.setCharacterAttributes(styleAttributes, false);
textPane.replaceSelection(String.valueOf((char) b));
// (there is no selection, so this inserts the char)
inputHistory.appendCodePoint(b);
if (inputHistory.toString().endsWith(CLEAR_SCREEN)) {
textPane.setText("");
inputHistory = new StringBuilder();
}
}
}
//////////////////////////////////////////////////////////////////////////
public static void main(String[] args) { // quick test/demo
try {
ConsoleWindow.withInput();
System.out.println("cls\nSome output");
System.err.println("Some error output");
Thread.sleep(1000);
System.out.println("cls");
System.out.print("Enter something: ");
Scanner sc = new Scanner(System.in);
System.out.println("Input was: " + sc.nextLine());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Edited by JamesCherrill
Zirely 16 Newbie Poster
rproffitt thanks for the advice, I will do it. Thanks.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.