Lottal输入输出小程序

本文介绍了使用Java进行随机数生成的方法,包括去除重复数字的技巧,并展示了如何通过随机数填充数组及对数组进行随机排序的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >




import java.util.Hashtable;
import java.util.Random;

public class Lottal {

public static void main(String[] args) {
Hashtable hashtable = new Hashtable();
Random rm = new Random();
int RmNum = 10;
for (int i = 0; hashtable.size() < RmNum; i++) {
int nValue = rm.nextInt(100);
if (!hashtable.containsValue(nValue) && nValue != 0)// 去重
{
hashtable.put(nValue, nValue);
}
}
System.out.println(hashtable.values());
}
}
----------------------------------------------------
import java.lang.reflect.Array;
import java.util.Random;

public class Lottal {
private int num;
private int length;
private int[] array;
public Lottal(int num, int length) {
this.num = num;
this.length = length;
}
public void getRandom() {
Random rnd = new Random();
array = new int[length];

for (int i = 0; i < length; i++) {
int rndNum = rnd.nextInt(num + 1);
array[i] = rndNum;
System.out.println(array[i]);
}
}
public String getLottalResult() {

for (int i = 0; i < num - 1; i++) {
System.out.println();
}
return array.toString();
}
public static void main(String[] args) {
Lottal lottal = new Lottal(3, 3);
lottal.getRandom();
}
}

---------------------------------
Hashtable hashtable = new Hashtable();
Random rm = new Random();
int RmNum = 100;
for (int i = 0; hashtable.Count < RmNum; i++)
{
int nValue = rm.Next(100);
if (!hashtable.ContainsValue(nValue) && nValue != 0)//去重
{
hashtable.Add(nValue, nValue);
Console.WriteLine(nValue.ToString());
}
}
----------------------------------


import java.util.Random;

public class testRandom {

public static void main(String[] args) {
Random test = new Random();
int[] a = new int[10];
int index;
for (index = 0; index < a.length; index++) {
a[index] = index + 1;
}
index--;
int tempIndex, tempContent;
for (; index >= 0; index--) {
tempIndex = test.nextInt(a.length);
tempContent = a[index];
a[index] = a[tempIndex];
a[tempIndex] = tempContent;
}

for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}

----------------------------------
args[]

1.Scanner s = new Scanner(System.in);
int i = s.nextInt();
2.
BufferedRader b = new BufferedReader(new InputStreamReader(System.in));

int i = Integer.parseInt(b.readLine());

一般我用这两个。

--------------------
s.nextInt()表示只接受int的数据。
还有nextDouble()接收double的数据

因为你输入的内容可能有很多种数据类型,s.nextInt()就自动把你输入的数据转化为int型


import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;


public class Tongxuelu {
public static void main(String[] arg) throws Exception {
String name2;
String phonenumber;
for (int a = 0; a < 5; a++) {
System.out.println("请输入你的名字");
BufferedReader name1 = new BufferedReader(
new InputStreamReader(System.in));
name2 = name1.readLine();
System.out.println("请输入你的电话号码");

phonenumber = name1.readLine();
System.out.print("姓名");
System.out.println(name2);
System.out.print("电话号码");
System.out.println(phonenumber);

try {
File tongxue = new File("tongxuelu.txt"); //文件的名
PrintWriter writer = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(tongxue, true)));
writer.println("姓名:" + name2 + "\t" + "电话号码:" + phonenumber); //写入文件


writer.flush(); //刷新
writer.close(); //关闭流

} catch (Exception e) {
e.printStackTrace();

}

}

}

}


实现输入名字和号码,显示你所输入的信息。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值