(循环语句+分支语句)编程实现 自从知道自己是井底之蛙之后青蛙决定跳出深井 假设井深20.01米 青蛙白天可以向上爬5米 夜晚回落一半 青蛙的耐心和体力能维持一周 若青蛙最终能跳出井口则打印输出”天蓝蓝秋草香“ 否则打印输出”跳不出的深井“
解题思路:1)定义三个变量分别用于记录井深和青蛙当前的高度和天数
2)使用循环语句 若青蛙当前的高度<井深 则一直执行3)
3) 天数增加 重新计算青蛙向上后的高度 并判断是否>井深 若大于则结束循环 否则再次计算青蛙回落后的高度
4)天数跟一周进行比较后打印输出结果
/**逆序扫描给定数据,将无序数列中的最小元素依次排到最前端。即先冒出最小的气泡*/
public static int[] BubbleSort2(int[] array){
int t = 1;
for(int i = 0 ; i < array.length - 1 ; i++){
for(int j = array.length - 1 ; j > 0 ; j--){
if(array[j-1] > array[j]){
t = array[j];
array[j] = array[j-1];
array[j-1] = t;
}
}
// for (int a : array) {
// System.out.print(a+" ");
// }
// System.out.println();
}
return array;
}
问题解答:
// 实现循环语句和分支语句的编程实现
public class JumpOut {
public static void main(String[] args) {
double height = 20.01;
double up = 5;
double down = up / 2;
double total = 0;
int days = 0;
while (true) {
if (total + up >= height) {
System.out.println("天蓝蓝秋草香");
break;
}
total += up;
days++;
if (days == 7) {
System.out.println("跳不出的深井");
break;
}
total -= down;
}
}
}
// 定义一个类,该类中包含以下几个方法
public class Methods {
public static String[] reverseOrder(String[] strs) {
String[] result = new String[strs.length];
for (int i = 0; i < strs.length; i++) {
result[i] = strs[strs.length - i - 1];
}
return result;
}
public static int[] intersection(int[] nums1, int[] nums2) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < nums1.length; i++) {
for (int j = 0; j < nums2.length; j++) {
if (nums1[i] == nums2[j] && !list.contains(nums1[i])) {
list.add(nums1[i]);
break;
}
}
}
int[] result = new int[list.size()];
for (int i = 0; i < result.length; i++) {
result[i] = list.get(i);
}
return result;
}
public static double[] union(double[] nums1, double[] nums2) {
ArrayList<Double> list = new ArrayList<>();
for (int i = 0; i < nums1.length; i++) {
if (!list.contains(nums1[i])) {
list.add(nums1[i]);
}
}
for (int j = 0; j < nums2.length; j++) {
if (!list.contains(nums2[j])) {
list.add(nums2[j]);
}
}
double[] result = new double[list.size()];
for (int i = 0; i < result.length; i++) {
result[i] = list.get(i);
}
return result;
}
}
// 实现判断回文字符串的功能
import java.util.Scanner;
public class PalindromicString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
if (input.equals(new StringBuffer(input).reverse().toString())) {
System.out.println("是回文字符串");
} else {
System.out.println("不是回文字符串");
}
}
}
// 实现5次机会的猜数字小游戏
import java.util.Scanner;
import java.util.Properties;
import java.io.IOException;
import java.io.FileReader;
import java.io.FileWriter;
public class GuessNumber {
public static void start() {
Scanner scanner = new Scanner(System.in);
int target = (int) (Math.random() * 101);
int times = 0;
while (times < 5) {
int input = scanner.nextInt();
if (input == target) {
System.out.println("猜对了!");
return;
} else if (input < target) {
System.out.println("猜小了!");
} else {
System.out.println("猜大了!");
}
times++;
}
System.out.println("5次机会以用尽,请付费试玩");
Properties prop = new Properties();
try {
FileReader fr = new FileReader("count.txt");
prop.load(fr);
fr.close();
int count = Integer.parseInt(prop.getProperty("count", "0"));
prop.setProperty("count", String.valueOf(count + 1));
FileWriter fw = new FileWriter("count.txt", false);
prop.store(fw, "");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Game {
public static void main(String[] args) {
Properties prop = new Properties();
try {
FileReader fr = new FileReader("count.txt");
prop.load(fr);
fr.close();
int count = Integer.parseInt(prop.getProperty("count", "0"));
if (count >= 5) {
System.out.println("试玩已结束,请付费");
return;
}
} catch (IOException e) {
e.printStackTrace();
}
GuessNumber.start();
}
}
// 统计字符串中的奇数和偶数的个数
import java.util.Scanner;
public class CountOddAndEven {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int even = 0;
int odd = 0;
for (int i = 0; i < input.length(); i++) {
int num = input.charAt(i) - '0';
if (num % 2 == 0) {
even++;
} else {
odd++;
}
}
System.out.println("偶数的个数是:" + even);
System.out.println("奇数的个数是:" + odd);
}
}
以上就是使用Java语言实现循环语句和分支语句的编程实现的解答。