「java报数游戏」java猜数字游戏

博主:adminadmin 2023-01-28 14:21:08 1100

本篇文章给大家谈谈java报数游戏,以及java猜数字游戏对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java编程 有n个人围成一个圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出

public class Increase {

public static void rep(boolean[] people) {

int i = 0,j=0,n=people.length,m=n;

while(n2){

i=++i%m;

if (people[i] == true){

j++;

if (j==3){

people[i] = false;

System.out.println(i);

n--;//总人数减1

j = 0;//到3从头数

}

}

}

}

public static void main(String[] args) {

boolean people[] = new boolean[10];

for(int i = 0; i10; i++){

people[i] = true;

}

rep(people);

}

}

main函数为测试例子,打印结果如下

3

6

9

2

7

1

8

5

JAVA编写一群人围成一圈报数从一开始一直报道99凡事遇到七的倍数或含有七的数?

public static void main(String[] args) {

System.out.println("开始报数:");

for (int i = 1; i 100; i++) {

if (i % 7 == 0 || (i+"").indexOf("7")0){

System.out.print("\n@");

}else{

System.out.print(" "+i);

}

}

System.out.println("报数结束!");

}

(Java 语言)有 n 个孩子站成一圈,从第一个孩子开始顺时针方向报数,报到 3 的人出列,下一个人继续从 1

.add(a.remove(0));

正确。

for(int k=0; k2; k++)

________________;

处没有括弧,而且只有一个空,不能填写两行语句。

思路:

1. 由于a.remove(0); 明显是要移除出列的孩子,也就是报3的。但是remove的function是移除Index是0的元素,也就是第一个人。所以第一点就是要把3的放在最前面。就一定要把3之前的重新排序,简单就是要放在最后。

2. 另一个考点就是要知道Vector 中remove(int index)这个function的返回,是元素对象,而不是一般人认为的void.

public E remove(int index)移除此向量中指定位置的元素。将所有后续元素左移(将其索引减 1)。返回:

移除的元素。

用java实现switch报数游戏

/**

 * 根据题目的提示:“从a开始,每报14个数,便又回到A”, 那么报数的顺序是:先是a-h,然后再h-a --------“abcdefghgfedcba”

 * 

 * @author Kong

 * 

 */

public class SwitchGame {

private static int num = 0;

private static final int PLUS = 0;// 正数标记

private static final int MINUS = 1;// 负数标记

public static void main(String[] args) {

char[] arr = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };// 数组;

int target = 8411250;// 目标数字;

int i = 0;// 开始索引值0就是'a';

int flag = PLUS;// 一个标记,用于确定遍历数组的方向;

while (num  target) {// 当报的数小于目标数时执行;

number(arr[i]);// 报数方法;

if (i == arr.length-1) {// 当索引值等于7即遍历到‘h’,就倒过来遍历的标记即 i--

flag = MINUS;

} else if (i == 0) {// 当索引值等于0即到了‘a',就正方向遍历即 i++

flag = PLUS;

}

if (flag == PLUS) {// 当标记为正

i++;

} else if (flag == MINUS) {// 当标记为反

i--;

}

}

}

/**

 * 报数;不过我觉得这里可以不用switch,可能我理解错了题目

 * 

 * @param n

 */

private static void number(char n) {

String str = "";

switch (n) {

case 'a':

str = "a";

break;

case 'b':

str = "b";

break;

case 'c':

str = "c";

break;

case 'd':

str = "d";

break;

case 'e':

str = "e";

break;

case 'f':

str = "f";

break;

case 'g':

str = "g";

break;

case 'h':

str = "h";

break;

default:

break;

}

num++;

System.out.println(str + "\t" + num);

}

}

这是目标数为20的运行结果,看下是不是你想要的(代码有点乱,不过都写了注释了)

在Java中使用switch实现报数游戏

在 Java 7之前,switch 只能支持 byte、short、char、int或者其对应的封装类以及 Enum 类型。在 Java 7中,String支持被加上了。pre t="code" l="java"switch (ctrType) {

case ;01; :

exceptionType = ;读FC参数数据;;

break;

case ;03; :

exceptionType = ;读FC保存的当前表计数据;;

break;

default:

exceptionType = ;未知控制码:;+ctrType;

}其中ctrType为字符串。如在jdk 7 之前的版本使用, 会提示如下错误:Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum variables are permitted意为jdk版本太低,不支持。

请用JAVA编一程序100个人围成一个圆圈从编号1开始报数到18号时该人出局,下一个重新从1开始如此循环到最后

public class Count18 {

public static void main(String[] args) {

int[] a = new int[100];

for(int i=0; ia.length; i++) {

a[i] = 1;

}

int leftCount = 100;

int countNum = 0;

int index = 1;

while(leftCount != 0) {

if(a[index-1] == 1) {

countNum ++;

if(countNum == 18) {

System.out.print(index + " ");

countNum = 0;

a[index-1] = 0;

leftCount --;

}

}

index ++;

if(index-1 == a.length) {

index = 1;

}

}

}

}

哈哈,一个问题发了两个帖子呢,那就让俺捡下便宜吧。。。有什么问题hi我,希望对楼主有帮助,采纳吧。两个问题呢,虽然没有分也值了,没白忙活。。嘿嘿。

结果是这样:18 36 54 72 90 8 27 46 65 84 3 23 43 63 83 4 25 47 68 89 11 33 56 78 100 24 49 73 96 20 45 71 97 22 51 77 5 32 60 88 16 48 79 9 39 70 2 37 69 6 40 76 13 52 87 28 64 7 50 92 34 81 26 74 19 67 21 80 31 91 44 1 62 35 98 66 42 15 95 85 61 58 57 59 82 94 14 41 99 53 29 17 38 93 86 30 75 55 12 10

关于java报数游戏和java猜数字游戏的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。