Package Practicego;import java.util.scanner;/* * 6, the computer randomly generated a number of 0~9, the user to guess the number.The value is correct until it is guessed correctly. */public class Cto {public static void main (string[] args) {Scanner sc = new Scanner (system.in), while (true) {System.out.pri NTLN ("Guess number:"), int pernum = Sc.nextint (), int sysnum = (int) (Math.random () *10), if (Sysnum = = Pernum) {System.out.println (" Congratulations, you guessed it!! "); break;}}}Test
Java programming: add custom attributes to enumeration (print the names and prices of each apple using a for-each loop)
Package enumeration; // Add the Custom Attributes enum Apple {AURORA (10), BELMAC (12), CORTLAND (15), EMPIRE (8), GRAVENSTEIN (11) to the enumeration ); private int price; // constructor Apple (int price) {this. price = price;} int getPrice () {return price;} public class ApplesEnum {pub
Eclipse solves the Unhandled event loop exception error when editing the java file. eclipseunhandled
You can try:
It has nothing to do with Baidu anti-virus. It is a problem with the myeclipse editor. Myeclipse-> window-> preferences-> general-> editors-> file associationsSelect JSP and select the following jsp editor (myeclipse visual jsp designer is used in this case). You can change it to
Package Javafirst;public class Homework {public static void main (string[] args) {System.out.println ("Output a diamond! "); for (int i = 0; I Java loop output a diamond and factorial countdown
Java for Each loop array object traversal
Grammar
for (type Itr-var:iterableobj) statement-block
Take a look at the enumeration group instances
public class MainClass {public static void Main (string args[]) {int nums[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};for (int x:nums) {System.out.print (x + "");x = x * 10; No effect on Nums}System.out.println ();
for (int x:nums)System.out.print (x + "");
each row has been assigned.//The current value--the number of the column on the previous row + the first number on the left of the column on the previous line (see image)Arr[i][j]=arr[i-1][j]+arr[i-1][j-1]; } } //Print Output for(inti = 0; i ) { for(intj = 0; J ) {System.out.print (Arr[i][j]+ "\ T"); No Line break} System.out.println (); Line Change}}}Operation Result:1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 5 1
In general, access to data in List,array,set is accessed in the following wayint [] Nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, ten }; for (int i = 0; i ) { System.out.println ("i =" + i);}Since Java1.5, there has been a more convenient way to access: foreachint [] Nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, ten }; for (int x:nums) { System.out.println ("Value is:" + x); + = x;}
Grammar:
For (type variable name: Collection variable name) {statements;}
Precautions
array: The subscript value cannot be easily accessed. Collection: It is not easy to delete things in the collection compared to using Interator. inside is also called InteratoIn addition to simply traversing and reading out the contents, it is not recommended to use the enhanced for. "Program Analysis"int[] arr = {1,2,3,4,5};for (int i:arr) {System.out.println (i); }Collection C = new ArrayList ();C.add (New String ("AA"));C.add (New String ("BB"));For (Object o:c) {System.out.println (o); }
Tag: the base port exti new Int () defines the ring stringhttp://www.verejava.com/?id=16992623980921/* do{ }while(判断表达式) : 直到 while 判断表达式为 false 退出循环*/import java.util.Scanner;public class Test1 { /** * @param args the command line arguments */ public static void main(String[] args) { // 模拟切水果游戏, 键盘输入 1:西瓜,2:香蕉,3:水蜜桃,-1:雷 //输入数字打印对应的水果, 如果输入-1 终止游戏输入 Scanner in=new Scanner(System.in); int num=0; //定义了一个变量 do { num=in.n
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.