用Java编写一个程序
public class Test {
public static void main(String[] args) {
while (true) {
System.out.print("请输入姓名:");
Scanner input = new Scanner(System.in);
String name = input.next();
System.out.println("请输入的姓名是:"+name);
}
}
}
public class Du1 {
public static void main(String[] args) {
int year = 2011;
if((year % 4 == 0 && year % 100 != 0) || (year % 4 == 0 && year % 100 == 0) ){
System.out.println("是闰年");
}else{
System.out.println("It is ");
}
}
}
import javax.swing.JFrame ;
import javax.swing.JList ;
import java.util.Vector ;
import javax.swing.BorderFactory ;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel ;
import javax.swing.JScrollPane ;
import javax.swing.AbstractListModel ;import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container ;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout ;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter ;
import java.awt.event.WindowEvent ;
import javax.swing.event.ListSelectionListener ;
import javax.swing.event.ListSelectionEvent ;
class MyList extends JFrame implements ListSelectionListener,ActionListener{
private JFrame frame = new JFrame("Welcome To MLDN") ;
private Container cont = frame.getContentPane() ; // 得到窗体容器
private JList list1 = null ;
private MyPanel jp1=null;//画图形的面板
private JPanel jp2=null;//放JList
private JPanel jp3=null;//放按钮的面板
private JButton red=null;//红色按钮
private JButton green=null;//绿色按钮
public static Color c=Color.BLACK;
public static String shape=null;
public MyList(){
this.setLayout(new BorderLayout()) ; // 定义排版
this.list1 = new JList(new MyListModel()) ; this.list1.setBorder(BorderFactory.createTitledBorder("形状")) ; this.list1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION) ;// 单选
//1
jp1=new MyPanel();
this.add(jp1,BorderLayout.CENTER);
//2
jp2=new JPanel();
jp2.add(list1) ;
this.add(jp2,BorderLayout.WEST);
//3
jp3=new JPanel();
red=new JButton("红色");
red.addActionListener(this);
green=new JButton("绿色");
green.addActionListener(this);
jp3.add(red);
jp3.add(green);
this.add(jp3,BorderLayout.SOUTH);
this.setSize(500,300) ;
this.setVisible(true) ;
this.list1.addListSelectionListener(this) ;
} public void valueChanged(ListSelectionEvent e){ // 事件处理
int temp[] = list1.getSelectedIndices() ;
for(int i=0;i<temp.length;i++){
String value=(String) list1.getModel().getElementAt(temp[i]);
if(value!=null){
shape=value;
this.repaint();
}
}
} @Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Object o=arg0.getSource();
if(o==red){
c=Color.RED;
this.repaint();//重绘
}else if(o==green){
c=Color.GREEN;
this.repaint();
}
}}class MyListModel extends AbstractListModel{
private String shape[] = {"圆形","扇形","矩形"} ;
public Object getElementAt(int index){
if(index<this.shape.length){
return this.shape[index] ;
}else{
return null ;
}
}
public int getSize(){
return this.shape.length ;
}
}class MyPanel extends JPanel{
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.CYAN);//背景颜色
g.fillRect(10,10, 400, 300);//背景区域
g.setColor( MyList.c);
String shape=MyList.shape;
if(shape==null){
g.drawRect(150, 50,130, 140);//初始图形
}else if("圆形".equals(shape)){
g.fillOval(100, 100, 60, 60);
}else if("扇形".equals(shape)){
g.fillArc(100, 50, 260, 260,10, 80);
}else if("矩形".equals(shape)){
g.fillRect(150, 50,130, 140);
}
}
}public class Test{
public static void main(String args[]){
new MyList() ;
}
} //程序运行结果
写得有点乱,希望对你有帮助。
package common;
public class Engumber {
private static final String[] LT20 = new String[]{
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve","thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
};
private static final String[] TNS = new String[]{
"twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"
};
private static final String[] THS = new String[]{
"hundred","thousand"
};
private static final String $ = " and ";
private static String lessThan20Converter(int num){
return LT20[num];
}
private static String from20To99Converter(int num){
int b = num;
int h = num/10;
return b==0 ? TNS[h-2] : TNS[h-2] "-" lessThan20Converter(b);
}
private static String moreThan99Converter(int num){
int high = num/100;
int low = num0;
int th = high/10;
int hu = high;
String ret = "";
if(th!=0){
ret = LT20[th] " " THS[1] " ";
}
ret = LT20[hu] " " THS[0] $ from20To99Converter(low);
return ret;
}
public static String convert(int num){
if(num<0 || num>9999){
throw new IllegalArgumentException(num " is not in [0,9999].");
}
String ret = null;
if(num<20){
ret = lessThan20Converter(num);
}else if(num>=20
。使用JAVA编写一个程序求出一个整数的所有因子的和
以下是一个使用Java编写的程序,用于求出一个整数的所有因子的和:在这个程序中,首先从用户那里获取一个整数输入。然后,使用一个循环来遍历从1到该整数的所有整数。对于每个整数,如果它是该整数的因子(即该整数除以该整数的余数为0),则将该整数加到总和中。最后,输出所有因子的和。例如,如果用户...
如何用java语言编写一个程序?
clc;clear;n=randi([10,99],[1,20])%产生随机数,在10到99之间的均匀随机数 a=mean(n)%计算平均数 n(find(n
要求编写一个java程序,输入学生信息,并能保存与显示学生信息。_百度知 ...
import java.util.ArrayList;import java.util.Scanner;import com.sun.org.apache.xpath.internal.Arg;public class Student { \/\/两个私有属性 private int no ;private String name ;\/\/默认构造函数 public Student(){} \/\/带参构造函数 public Student(int no,String name){ this.no=no;this.nam...
怎样用JAVA编写一个程序:
\/\/1.创建一个学生类,要求具有姓名、年龄、学号、密码、性别、对象的创建时间几个属性public class Students { private int stu_id;private String stu_name;private int stu_age;private String stu_pwd;private String stu_sex;private String stu_date;public Students() {} public Students(int id...
用Java编写一个程序
import java.awt.Color;import java.awt.Container ;import java.awt.Font;import java.awt.Graphics;import java.awt.GridLayout ;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter ;import java.awt.event.WindowEvent ;import javax.swing....
如何用java编写一个循环程序?
import java.util.*;public class Main { public static void main (String [] args) { Scanner s=new Scanner(System.in);int sum=0;int x=s.nextInt();while(x!=0) { if(x%2==1) { sum+=x;} x=s.nextInt();} System.out.println(sum);} } 运行效果:...
用java写一个程序?
以下是一个使用Java语言编写的程序,可以计算出20000000~300000000之间所有的素数,并将找到的素数写入primefile.dat文件:javaCopy code import java.io.FileOutputStream; import java.io.IOException; public class PrimeNumberCalculator { public static void main(String[] args) { int start = 20000000...
(用JAVA语言)编写一个程序用选择法对数组a[]={20,10,50,40,30,70,60...
public static void main(String args[]) { int arr[] = {20,10,50,40,30,70,60,80,90,100};System.out.println("数组由大到小排序前:");for(int num : arr) { System.out.print(num + "、");} \/\/使用冒泡排序法,将数组由大到小进行排序 for(int i = 0;i < arr.length-...
如何编写第一个java程序
恭喜你,你成功编写并运行了第一个Java程序。打开Demo文件夹下的bin目录,出现了一个 HelloWorld.class 文件,这就是编译生成的字节码。与C、C++不同,Java编译的结果不是可执行文件,而是字节码文件。字节码文件不能直接运行,必须由JVM翻译成机器码才能运行,这就是为什么运行Java程序必须安装JVM的原因...
编写一个JAVA程序,定义两个整型变量a,b并给它们赋初值,然后找出它们...
\/\/下面是手动输入的方法 import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("输入数a和数b"); int a = sc.nextInt(), b = sc.nextInt(); sc.close(); if (a > b) ...
温辉可成: 一.创建Java应用程序源文件1)打开文本编辑器NotePad(记事本) 【开始】→【程序】 →【附件】 →【记事本】2)在该新建文件中,输入下述代码: public class HelloWorld { public static void main(String[] args) { System.out.println...
永顺县13895232969: 用Java编写一程序 - ?
温辉可成: 完整程序见下,记得采用啊:import java.util.*; class MinusException extends ArithmeticException { public MinusException() { } public MinusException(String str) { super(str); } } class OverException extends ArithmeticException { public ...
永顺县13895232969: 用Java编写一个程序 - ?
温辉可成: import java.util.*; public class TestMonth{ void show(int month,int year){ Calendar obj = Calendar.getInstance(); String[] weekday = {"日","一","二","三","四","五","六"}; int[] monthday = {31,28,31,30,31,30,31,31,30,31,...
永顺县13895232969: 用JAVA写一个程序 - ?
温辉可成: import java.util.Arrays;import java.util.Scanner;public class T { public static void main(String[] args) { ...
永顺县13895232969: 用java编写程序?
温辉可成: import java.util.Scanner; public class CalcByDigitally { public static void main(String[] args) { Scanner input = new Scanner(System.in); int num; int sum = 0; System.out.print("输入一个整数:"); num = input.nextInt(); System.out.print(num); ...
永顺县13895232969: 用java程序编写一个程序,帮帮忙 - ?
温辉可成: private static String str = "how Do you do"; // 定义一个字符串,用来装转换之后的结果 String ss = null; // 把字符串转换成字符数组 char c[] = str.toCharArray(); // 把字符数组的第一个字符转换成字符串 ss = new String(c, 0, 1); // 把字符串...
永顺县13895232969: 编写一个JAVA简单的程序! - ?
温辉可成: public static void main(String[] args){String firstString="This is";String secondString="This is"; if(firstString==null && secondString==null){System.out.println("①firstString 与 secondString 相等");}else if(firstString!=null && firstString...
永顺县13895232969: 用java编写一个程序,谁能帮个忙??
温辉可成: public class Sushu { public static void main(String[] args) { print(10); } public static void print(int i){ System.out.print(i+"的素数因子有:"); for(int j=2;j<i/2+1;j++){ if((i % j==0) && isPrime(j)){ System.out.print(j+","); } } System.out.print("\n"); } ...
永顺县13895232969: 用JAVA编写一个应用程序 - ?
温辉可成: //Lader类 class Lader { double shangDi; //上底 double xiaDi; //下底 double heigth; //高 double area; //面积 //下面是构造方法 public Lader(double shangDi,double xiaDi,double heigth){ this.shangDi=shangDi; this.xiaDi=xiaDi; this.heigth=heigth...
永顺县13895232969: 用Java编写一个程序如下: - ?
温辉可成: 三边数值之间用用空格隔开 import java.util.Scanner; public class Triple { public static void main(String[] args) { double[] numbers = getNumbersFromInput(); boolean isValidateTriple = valieDateNums(numbers[0], numbers[1], numbers[2]); if(...