Receive character type and string type data from keyboard (scissors stone cloth)

Source: Internet
Author: User

Define a rule for scissors and stone cloth in the program, then define two character variables, then receive two data from the keyboard and assign them to the two variables, then compare them according to the established rules, and then output the results.

.
This needs to first import the Scanner class: Import Java.util.Scanner;, define a Scanner object,
Scanner p = new Scanner (system.in);
Then define a string variable that receives the contents of the string from the keyboard,
String P1 = P.next ();
The great God said, to receive the integer variable is used. Nextint; To receive a string type, use. Next (), if you want to receive a double variable, use the. nextdouble ()
The first letter of the string is then assigned to the character variable, which needs to be used. CharAt (0);
Char play1 = p1.charat (0);
Then we can compare them.

(in addition, Char is just a basic type, and string is a class that can be referenced directly.)
such as Char c= ' C '; The method cannot be called directly on C.
String s= "ABC"; This can be called S.charat (0), and so on, because string is a class, which is the invocation of the object)

(about. charAt (0),

String str = "ABC";
char ch = str.charat (0);
Char CH2 = Str.charat (1);

At this time Ch is a,ch2 is b;)
The complete code of this program is as follows:
Import Java.util.scanner;public class Test02{public static void Main (String args[]) {Scanner p = new Scanner (system.in); System.out.println ("Player1 First:"); String P1 = P.next (), char play1 = P1.charat (0); System.out.println ("Player2 please:"); String P2 = P.next (), char play2 = P2.charat (0), if ((Play1 = = ' s ' |play1 = = ' J ' |play1 = = ' B ') & (Play2 = = ' s ' |play2 = = ' J ') |play2 = = ' B ') {if (play1 = = play2) {System.out.println ("flat");} else if (play1 = = ' s ') {if (Play2 = = ' J ') {System.out.println ("Player1 wins");} Else{system.out.println ("Player2 wins");}} else if (play1 = = ' J ') {if (Play2 = = ' B ') {System.out.println ("Player1 wins");} Else{system.out.println ("Player2 wins");}} else if (play1 = = ' B ') {if (Play2 = = ' s ') {System.out.println ("Player1 wins");}}} ELSE{SYSTEM.OUT.PRINTLN ("input Error");}}}

  

Receive character type and string type data from keyboard (scissors stone cloth)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.