看代码:
import java.util.ArrayListimport java.util.Map
import java.util.Scanner
import java.util.TreeMap
public class Main {
public static void main(String[] args) throws Exception {
try (Scanner in = new Scanner(System.in)) {
int key, value
// TreeMap 是可以根据键值进行自动灶哪纳排序的 Map
// 一个键可能对应多个值,我们使用 ArrayList 来保存这多个值
缓帆 TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>()
while (true) {
key = in.nextInt()
value = in.nextInt()
if (0 == key && 0 == value) { // 两个都是 0 跳出循环
break
}
if (null == map.get(key)) { // 当前 key 没有对应的集合
ArrayList<Integer> values = new ArrayList<>()
隐没 values.add(value)
map.put(key, values)
} else {
map.get(key).add(value)
}
}
for (Map.Entry<Integer, ArrayList<Integer>> entry : map.entrySet()) {
System.out.printf("%d %d", entry.getKey(), entry.getValue().size())
for (Integer v : entry.getValue()) {
System.out.printf(" %d", v)
}
System.out.println()
}
}
}
}
运行:
可以用String的indexof(str,fromindex)方法,循环遍历加一个计数器统计次数。
public class CountTimes {public static 旁枯void main(String[] args) {
String str="In the entire world there's 森启源nobody like me and my best wishes is become myself"
int times = searchstr("my", str) //返回2
System.out.println(times)
}
public static int searchstr(String key, String str) {
int index = 0//每次的搜索到的下此态标
int count = 0//计数器
while (( index=str.indexOf(key, index)) != -1) {
index = index + key.length()
count++
}
return count
}
}
写3个全局变量(A总数,B输岩渣,C赢),没调用一岩枣卖次猜拳函数就A++,判断一下,赢了,A++,粗逗B++,C不动;输了A++,C++,B不动最后那3个变量就是要统计的
public int addNum() throws IOException{
FileInputStream fin=new FileInputStream("num.data")
DataInputStream din=new DataInputStream(fin)
int i=din.readInt()
i++
FileOutputStream fout=new FileOutputStream("num.data")
DataOutputStream dout=new DataOutputStream(fout)
dout.writeInt(i)
return i
}
num.data里需要先用OutputStream写进去一个0
执行以下代码
public static void main(String args[]) {
FileOutputStream fout
try {
fout = new FileOutputStream("num.data")
DataOutputStream dout=new DataOutputStream(fout)
dout.writeInt(0)
} catch (FileNotFoundException e1) {
e1.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)