求java编程的代码
代码如下,望采纳
public class PrintPrime{
public static void main(String args[]){
//设置一个计数变量count,用于统计一行当中已经输出数字的个数
int count = 0;
//写代码时人为判断200为非素数,如果不考虑题目的严格要求的话,可以写成200
for(int i = 100;i=200;i++){
//判断数字是否为素数,若是,则count+1并输出数字
if(PrintPrime.IsPrime(i)){
count++;
System.out.print(i+" ");
}
//如果一行十个已经输出完毕,计数归零,换行
if(count==10){
count=0;
System.out.println();
}
}
}
//判断数字是否为素数
public static boolean IsPrime(int n){
//如果小于等于三,则大于一即为素数
if (n = 3) {
return n 1;
}
//从2循环到数字的开平方,算法优化
for(int i=2;i=Math.sqrt(n);i++){
if(n%i == 0)
return false;
}
return true;
}
}
Java编程求代码!
Java code
class Vehicle
{
private int speed,size;
//Vehicle constructor
public Vehicle(int speed,int size)
{
this.speed=speed;
this.size=size;
System.out.println("Init speed: "+speed+",Size: "+size);
}
//set speed
public void setSpeed(int speed)
{
this.speed=speed;
System.out.println("Set speed!");
}
//speedUp
public void speedUp()
{
speed*=2;
System.out.println("Speed Up!");
}
//speedDown
public void speedDown()
{
speed-=5;
System.out.println("Speed Down!");
}
//move
public void move()
{
System.out.println("Is moving at speed: "+speed);
}
//main method
public static void main(String[] args)
{
Vehicle vce=new Vehicle(80,200);
vce.move();
vce.setSpeed(120);
vce.move();
vce.speedUp();
vce.move();
vce.speedDown();
vce.move();
}
}
Debug:
Java编程 代码如下
"13423"
foo(0) 时 output 值为"134"
foo(1) 诗 output 值为"13423"
因为finally无论出没出现异常都会要执行的,哪怕你写了return。所以finally语句块常用来“收尾”工作。
急!!!简单JAVA编程代码
public class Test {
public static void main(String[] args) {
MyRectangle rec = new MyRectangle(3, 5);
MyRectangle square = new MySquare(4);
System.out.println(rec.toString());
System.out.println(square.toString());
}
}
class MyRectangle{
protected double width;
protected double length;
public MyRectangle(double length, double width){
this.width = width;
this.length = length;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getArea(){
return this.width * this.length;
}
public String toString(){
return "长方形的长为:" + length + ", 宽: " + width + ", 面积为:" + getArea();
}
}
class MySquare extends MyRectangle{
public MySquare(double length){
super(length, length);
}
public double getArea(){
return Math.pow(super.width, 2);
}
public String toString(){
return "正方形边长为: " + super.length + ", 面积为: " + getArea();
}
}
----------测试
长方形的长为:3.0, 宽: 5.0, 面积为:15.0
正方形边长为: 4.0, 面积为: 16.0
谁能给一个Java程序代码我,要50行到100行就可以啦。最好有几行解释
给你一个前几天才帮人写的
“计算整钱兑零”。程序要求用户输入一个双精度数代表总元数,就会列出总值与其等价的1元币、二角五分币、5分币和1分币的数目。程序报告的数目是1元币的最大数、其次是二角五分币的最大数,等等,依此类推。只显示非零的单位。对单个单位显示单数单词,对多于一个单位的显示复数单词
import java.util.Scanner;
public class MoneyCalculate {
public static void main(String[] args) {
int max100 = 0;
int max25 = 0;
int max5 = 0;
int max1 = 0;
double money = getMoneyFromInput();
String str = String.valueOf(money).trim();
String[] ary = str.split("\\.");
max100 = Integer.parseInt(ary[0]);
if(ary.length == 2){
int fen = Integer.parseInt(ary[1]);
if(ary[1].trim().length() == 1){
fen = Integer.parseInt(ary[1]) * 10;
}
max25 = fen / 25;
if(fen % 25 != 0){
fen = fen % 25;
}else{
fen = 0;
}
max5 = fen / 5;
max1 = fen % 5;
}
StringBuilder sb = new StringBuilder(money + " = ");
if(max100 != 0){
sb.append(max100);
sb.append("*1 ");
}
if(max25 != 0){
sb.append(max25);
sb.append("*0.25 ");
}
if(max5 != 0){
sb.append(max5);
sb.append("*0.05 ");
}
if(max1 != 0){
sb.append(max1);
sb.append("*0.01 ");
}
System.out.println(sb.toString());
}
private static double getMoneyFromInput() {
Scanner scanner = new Scanner(System.in);
return scanner.nextDouble();
}
}
-----------
2.49
2.49 = 2*1 1*0.25 4*0.05 4*0.01
-----------
2.5
2.5 = 2*1 2*0.25
-----------
37.23
37.23 = 37*1 4*0.05 3*0.01
-----------------
123.569
123.569 = 123*1 22*0.25 3*0.05 4*0.01