Java Applet Example

Source: Internet
Author: User
Tags gcd integer numbers

This rookie just contact Java a week, to its understanding is not deep, can only intuitively feel it. The personal sense and C + + are syntactically similar (actually just pair), first using two sample programs to show the face of Java programs.

Example one: Classic GCD and LCM for two integer numbers. (The Euclidean algorithm is more typical)

Package LCM and GCD;

Import Java.util.Scanner;

public class Zuixiao {public
	static void Main (string[] args) {
		int a,b,m;
		Scanner s = new Scanner (system.in);
		System.out.print ("Please enter an integer:");
		A=s.nextint ();
		System.out.print ("Enter an integer again:");
		B=s.nextint ();
		Deff cd=new Deff ();
		M=cd.deff (a,b);
		int n=a*b/m;
		System.out.println ("GCD:" +m);
		System.out.println ("LCM:" +n);
	}
Class deff{Public
	int Deff (int x,int y) {
		int t;
		if (x<y) {
			t=x;
			x=y;
			y=t;
		}
		while (y!=0) {
			if (x==y) return x;
			else {
				int k=x%y;
				x=y;
				Y=k
			}
		}
		return x;
	}
}
The results of the run are as shown in the figure:

Example two: Enter the year, month, and day to determine the day of the week. Involved in the form of a message dialog box input and output, the effect is better than C + + (of course, can be input and output in a console, but the visual effects of No message dialog box pull wind). Please look at the code.

Package Zongheti;

Import Javax.swing.JOptionPane;
		public class Zongheti {public static void main (string[] args) {int h,q,m,j,k;
		String yearstriing = Joptionpane.showinputdialog (null, "Enter year", "Enter Demo", Joptionpane.question_message);
		
		Double year = double.parsedouble (yearstriing);
		String monthstring = Joptionpane.showinputdialog (null, "Enter Month", "Enter Demo", Joptionpane.question_message);
		
		Double month = double.parsedouble (monthstring);
		String daymonthstring = Joptionpane.showinputdialog (null, "Enter the number of days in a month", "Enter Demo", Joptionpane.question_message);

		Double daymonth = double.parsedouble (daymonthstring);
		j = (int) year/100;
		k = (int) year%100;
		m = (int) month;
		Q = (int) daymonth;
		if (m = = 1 | | | m = = 2) m+=12;
		H = (q+ (int) (26* (m+1)/10) +k+ (int) (K/4) + (int) (J/4) +5*j)%7;
		String day = "";
			Switch (h) {Case 0:day = "VI";
		Break
			Case 1:day = "Day";
		Break
			Case 2:day = "one";
		Break
			Case 3:day = "two";
		Break Case4:day = "three";
		Break
			Case 5:day = "four";
		Break
			Case 6:day = "five";
		Break
		String output = (int) year+ "year" + (int) month+ "The first of the month" + (int) daymonth+ "Days are weeks" +day;
	Joptionpane.showmessagedialog (Null,output, "Judgment of the Week", Joptionpane.information_message);
 }
}
The results of the run are as shown in the figure:


I believe that no contact with the Java children's shoes to see such a running effect, will feel the light bar, hurriedly from the foundation of the beginning to learn Java bar. Let you walk with me.

In addition to a simple Java and C/s + +: If you are engaged in the web and Internet industry, the main learning Java, while engaged in desktop system or service development, Linux and SCM industry, mainly learning C/s. Both languages are very widely used, and no one is better than that.

Related Article

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.