1、 设计Java程序
假设有50瓶饮料,喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?
2、 设计Java程序,实现如下功能:
获取50个0至300之间的随机整数,并输出到屏幕上;
取出上面50个整数中的偶数,倒序排列输出到屏幕上。
3、 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。
假设有50瓶饮料,喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?
2、 设计Java程序,实现如下功能:
获取50个0至300之间的随机整数,并输出到屏幕上;
取出上面50个整数中的偶数,倒序排列输出到屏幕上。
3、 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。
*假设有50瓶饮料,喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?
*/
public static int getDrinkNumber(int drinks){
int number = drinks;
while(true){
if(drinks<3){
break;
}
number += drinks/3;
drinks = drinks/3 + drinks%3;
}
return number;
}
/*
*获取50个0至300之间的随机整数,并输出到屏幕上;取出上面50个整数中的偶数,倒序排列输出到屏幕上
*/
public static void sort(){
Random random = new Random();
List list = new ArrayList();
for(int i = 0 ; i < 50; i++ ){
int temp = random.nextInt(300);
System.out.print(temp + " , ");
list.add(temp);
}
System.out.println();
Collections.sort(list);
for(int i = 49; i >=0; i--){
System.out.print(list.get(i) + " , ");
}
}
/*
*类设计
*/
class Vehicle{
private int wheels;
private int weight;
public Vehicle(int wheels, int weight){
this.wheels = wheels;
this.weight = weight;
}
public void print(){
System.out.println("wheels: " + wheels);
System.out.println("weight: " + weight);
}
}
class Car extends Vehicle{
private int loader;
public Car(int wheels, int weight, int loader) {
super(wheels, weight);
this.loader = loader;
}
public void print(){
super.print();
System.out.print("loader: " + loader);
}
}
class Truck extends Car{
private int playload;
public Truck(int wheels, int weight, int loader, int playload) {
super(wheels, weight, loader);
this.playload = playload;
}
public void print(){
super.print();
System.out.print("playload: " + playload);
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Collections;
class drinkNo
{
public static void main(String[] args)
{
ArrayList a = new ArrayList();
for(int a1=0;a1<50;a1++)
a.add("c");
int number=50,flag=0;
for(int a1=0;a1<a.size();a1++){
flag++;
if(flag==3){
number++;
System.out.println(number);
flag=0;
a.add("c");
}
if(a1==a.size()-1)
{
if(flag==2)
number++;
System.out.println("(允许赊账)结果是:");
System.out.println(number);
}
}
}}
*假设有50瓶饮料,喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?
*/
public static int getDrinkNumber(int drinks){
int number = drinks;
while(true){
if(drinks <3){
break;
}
number += drinks/3;
drinks = drinks/3 + drinks%3;
}
return number;
}
/*
*获取50个0至300之间的随机整数,并输出到屏幕上;取出上面50个整数中的偶数,倒序排列输出到屏幕上
*/
public static void sort(){
Random random = new Random();
List list = new ArrayList();
for(int i = 0 ; i < 50; i++ ){
int temp = random.nextInt(300);
System.out.print(temp + " , ");
list.add(temp);
}
System.out.println();
Collections.sort(list);
for(int i = 49; i >=0; i--){
System.out.print(list.get(i) + " , ");
}
}
/*
*类设计
*/
class Vehicle{
private int wheels;
private int weight; public Vehicle(int wheels, int weight){
this.wheels = wheels;
this.weight = weight;
} public void print(){
System.out.println("wheels: " + wheels);
System.out.println("weight: " + weight);
} } class Car extends Vehicle{
private int loader; public Car(int wheels, int weight, int loader) {
super(wheels, weight);
this.loader = loader;
} public void print(){
super.print();
System.out.print("loader: " + loader);
}
} class Truck extends Car{
private int playload;
public Truck(int wheels, int weight, int loader, int playload) {
super(wheels, weight, loader);
this.playload = playload;
} public void print(){
super.print();
System.out.print("playload: " + playload);
}
}
import java.util.List;
import java.util.Random;public class Test917 {
public static void main(String args[]){
int array[] = new int[50];
Random random = new Random();
/**
* 获取50个在0到300之间的随即整数
*/
for(int i=0;i<50;i++){
//不考虑重复的情况
// array[i] = (int) (random.nextDouble()*300);
array[i] = random.nextInt(300);
}
/**
* 输出这50个整数
*/
for(int i=0;i<50;i++){
System.out.print(array[i]+"\t");
if((i+1)%10==0){
System.out.print("\n");
}
if(i==49){
System.out.println("-------------------------------------------------------------------------------------");
}
} /**
* 获取所有偶数
*/
List list = new ArrayList();
for(int i=0;i<50;i++){
if(array[i]!=0&&array[i]%2==0){
list.add(array[i]);
}
}
int arraylast [] = new int[list.size()];
for(int i=0;i<list.size();i++){
arraylast[i] = ((Integer)list.get(i)).intValue();
}
/**
* 排序
*/
int substitute ;
for(int i=0;i<arraylast.length-1;i++){
for(int j=0;j<arraylast.length-1;j++){
if(arraylast[j]>arraylast[j+1]){
substitute = arraylast[j];
arraylast[j] = arraylast[j+1];
arraylast[j+1] = substitute;
}
}
}
/**
* 输出排序后结果
*/
int k = 0;
System.out.println(arraylast.length);
for(int i=arraylast.length-1;i>=0;i--){
k++;
System.out.print(arraylast[i]+"\t");
if((k)%10==0){
System.out.print("\n");
}
}
}
}
--------------------------------------------------------public class Bottle { static int bottles = 50; static int count = 3; static int sumBottles = 0; public static int getHowBottles() { int c = 0;
for (int i = 0; i < bottles; i++) {
sumBottles++;
c++;
if (c == 3) {
bottles++;
c = 0;
}
}
return sumBottles;
} public static void main(String[] args) { getHowBottles();
System.out.println(sumBottles);
}
}
-------------------------------------------------------------public class Test { static void test() { Random r = new Random();
System.out.println(r.nextInt(300));
} public static void main(String[] args) { for (int i = 0; i < 50; i++) {
test();
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;public class Test { static int test() { Random r = new Random();
return r.nextInt(300);
} static void sortAndPrint(List intList) { Object[] oArrays = intList.toArray(); int count = 0;
for (int i = 0; i < oArrays.length; i++) {
Object obj = null;
for (int j = (i + 1); j < oArrays.length; j++) {
int iCount = ((Integer) oArrays[i]).intValue();
int jCount = ((Integer) oArrays[j]).intValue();
if (iCount < jCount) {
obj = oArrays[i];
oArrays[i] = oArrays[j];
oArrays[j] = obj;
}
}
System.out.println(++count + ": " + oArrays[i]);
}
} @SuppressWarnings("unchecked")
public static void main(String[] args) { List intList = new ArrayList();
int count = 0;
System.out.println("50个300以内的随机数:");
for (int i = 0; i < 50; i++) {
int random = test();
System.out.println(++count + ": " + random);
if (random % 2 == 0) {
intList.add(random);
}
}
System.out.println('\n' + "50个随机整数中的偶数的倒序排列:");
sortAndPrint(intList);
}
}
public class Drink {
private int bottles = 50;
private int sumBottles = 0;
public void drink(int count ){
if(bottles/count ==0){
sumBottles +=bottles%count ;
}else{
sumBottles += count ;
bottles = bottles-2;
drink(count);
}
}
public int getSumBottles() {
return sumBottles;
}
public static void main(String[] args){
Drink drink = new Drink();
drink.drink(3);
System.out.println(drink.getSumBottles());
}
}
public class Vehicle { public int wheels; public int weight; public Vehicle(int wheels, int weight) { this.wheels = wheels;
this.weight = weight;
} public void PrintCarInfo() { System.out.println("这是一辆Vehicle。");
System.out.println("它有" + this.wheels + "个车轮。");
System.out.println("它的重量为" + this.weight + "吨。");
}
}public class Car extends Vehicle { public int loader; public Car(int wheels, int weight, int loader) { super(wheels, weight);
this.loader = loader;
} public void PrintCarInfo() { System.out.println("这是一辆Car。");
System.out.println("它有" + super.wheels + "个车轮。");
System.out.println("它的重量为" + super.weight + "吨。");
System.out.println("它最多可以载" + this.loader + "个人。");
}
}public class Truck extends Car { public int payload; public Truck(int wheels, int weight, int loader, int payload) { super(wheels, weight, loader);
this.payload = payload;
} public void PrintTruckInfo() { System.out.println("这是一辆Truck。");
System.out.println("它有" + super.wheels + "个车轮。");
System.out.println("它的重量为" + super.weight + "吨。");
System.out.println("它最多可以载" + super.loader + "个人。");
System.out.println("它的最大载重量为" + this.payload + "吨。");
} public static void main(String[] args) { Truck t = new Truck(4, 3, 2, 1);
t.PrintTruckInfo(); System.out.println(); Car c = new Car(4, 3, 2);
c.PrintCarInfo(); System.out.println(); Vehicle v = new Vehicle(4, 3);
v.PrintCarInfo();
}
}
// TODO 自动生成方法存根 int bnum=50;
int time=0;
while(true)
{
bnum-=3;
time++;
bnum++;
if(bnum<=2)
break;
}
System.out.print(time+" "+bnum);
}
public void testCom(int n) {
int b = 0; //剩下的瓶子
int count = 0;//表示瓶子的个数
while (n !=0) {
b = n - 3;
count++;
n = b + 1;
}
System.out.println(count+50);
} public static void main(String args[]) {
MyTest t = new MyTest();
t.testCom(50);
}}
public class O { /**
* @param args
*///假设有50瓶饮料,喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=50;
int i2=0;
int i3=0;
int i4=0;
i2=i/3;
i3=i%3+i2;
i4=i2;
while(i3>2){
i=i3;
i2=i/3;
i3=i%3+i2;
i4=i4+i2;
}
System.out.println("i4="+(i4+50));
}}
public class O { /**
* @param args
*///假设有50瓶饮料,喝完3个空瓶可以换一瓶饮料,依次类推,请问总共喝了多少瓶饮料?
public static void main(String[] args) {
// TODO Auto-generated method stub
int i=50;
int i2=0;
int i3=0;
int i4=0;
i2=i/3;
i3=i%3+i2;
i4=i2;
while(i3>2){
i=i3;
i2=i/3;
i3=i%3+i2;
i4=i4+i2;
}
System.out.println("i4="+(i4+50+i3));
}}
private static int empty=0;
public static void main(String []ared){
Count count=new Count();
count.get(50);
//总瓶数
int whole=empty+50;
System.out.println("总瓶数"+whole);
}
//递归计算可换瓶数
public int get(int x){
empty+=x/3;
x=x/3+x%3;
if(x<3)
return x;
else
return get(x);
}
}
饮料的那个,还算是比较短吧
class drink {
public static void main(String[] args) {
int drinks = Integer.parseInt(args[0]);
int Num = drinks;
while(drinks > 2) {
Num = Num + drinks / 3;
drinks=drinks / 3 + drinks % 3;
}
System.out.println(Num);
}
}
重新发一遍,刚才的格式没弄好O(∩_∩)O~
class drink {
public static void main(String[] args) {
int drinks = Integer.parseInt(args[0]);
int Num = drinks;
while(drinks > 2) {
Num = Num + drinks / 3;
drinks=drinks / 3 + drinks % 3;
}
System.out.println(Num);
}
}