1. 求JAVA语言的源代码参考...
每个汉字代表1个数字(1~9)
那i后面参考结果中的0是哪里来的
如果是代表0到9的数字我这里写了一个应该算是本方法的方法
public static void main(String[] args) {
String[] sai = {"0","1","2","3","4","5","6","7","8","9"};
String[] ruan = {"0","1","2","3","4","5","6","7","8","9"};
String[] jian = {"0","1","2","3","4","5","6","7","8","9"};
String[] bi = {"0","1","2","3","4","5","6","7","8","9"};
String[] pin = {"0","1","2","3","4","5","6","7","8","9"};
for (String saii : sai) {
for (String ruani : ruan) {
if(ruani.equals(saii))
continue;
for (String jiani : jian) {
if(jiani.equals(ruani) || jiani.equals(saii))
continue;
for (String bii : bi) {
if(bii.equals(jiani) || bii.equals(ruani) || bii.equals(saii))
continue;
for (String pini : pin) {
if(pini.equals(bii) || pini.equals(jiani) || pini.equals(ruani) || pini.equals(saii)){
continue;
}
String tmp1=saii+ruani+jiani;
String tmp2=bii+saii;
String tmp3=ruani+jiani+bii+pini;
int tmpI1=Integer.parseInt(tmp1);
int tmpI2=Integer.parseInt(tmp2);
int tmpI3=Integer.parseInt(tmp3);
int tmpI4=tmpI1*tmpI2;
if(tmpI4==tmpI3){
System.out.println(tmp1+"*"+tmp2+"="+tmp3);
}
}
}
}
}
}
}
2. 求:用JAVA语言编写的银行家算法的源代码
import java.util.*;
class ThreadTest {
static int type = 4, num = 10; //定义资源数目和线程数目
static int[] resource = new int[type]; //系统资源总数
//static int[] copyResource = new int[type]; //副本
static Random rand = new Random();
static Bank[] bank = new Bank[num]; //线程组
Bank temp = new Bank();
public void init() {
//初始化组中每个线程,随机填充系统资源总数
for(int i = 0; i < type; i++)
resource[i] = rand.nextInt(10) + 80;
System.out.print("Resource:");
for(int i = 0; i < type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
for(int i = 0; i < bank.length; i++)
bank[i] = new Bank("#" + i);
}
public ThreadTest4() {
init();
}
class Bank extends Thread {
//银行家算法避免死锁
public int[]
max = new int[type], //总共需求量
need = new int[type], //尚需资源量
allocation = new int[type]; //已分配量
private int[]
request = new int[type], //申请资源量
copyResource = new int[type]; //资源副本
private boolean isFinish = false; //线程是否完成
int[][] table = new int[bank.length][type*4]; //二维资源分配表
private void init() {
// 随机填充总共、尚需、已分配量
synchronized(resource) {
for(int i = 0; i < type; i++) {
max[i] = rand.nextInt(5) + 10;
need[i] = rand.nextInt(10);
allocation[i] = max[i] - need[i];
resource[i] -= allocation[i]; //从系统资源中减去已分配的
}
printer();
for(int i = 0; i < type; i++) {
if(resource[i] < 0) {
//若出现已分配量超出系统资源总数的错误则退出
System.out.println("The summation of Threads' allocations is out of range!");
System.exit(1);
}
}
}
}
public Bank(String s) {
setName(s);
init();
start();
}
public Bank() {
//none
}
public void run() {
try {
sleep(rand.nextInt(2000));
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
while(true) {
//程序没有完成时一直不断申请资源
if(askFor() == false) {
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
}
else
tryRequest();
if(noNeed() == true)
break;
}
//休眠一段时间模拟程序运行
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(getName() + " finish!");
synchronized(resource) {
//运行结束释放占有资源
for(int i = 0; i < type; i++) {
resource[i] += allocation[i];
need[i] = allocation[i] = max[i] = 0;
}
}
}
private void printer() {
//打印当前资源信息
System.out.print(getName() + " Max:");
for(int i = 0; i < type; i++)
System.out.print(" " + max[i]);
System.out.print(" Allocation:");
for(int i = 0; i < type; i++)
System.out.print(" " + allocation[i]);
System.out.print(" Need:");
for(int i = 0; i < type; i++)
System.out.print(" " + need[i]);
System.out.print(" Available:");
for(int i = 0; i < type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
}
private boolean askFor() {
//随机产生申请资源量并检测是否超标
boolean canAsk = false;
for(int i = 0; i < type; i++) {
request[i] = rand.nextInt(20);
//防止申请量超过所需量
if(request[i] > need[i])
request[i] = need[i];
}
for(int i = 0; i < type; i++) //防止随机申请资源全为0
if(request[i] > 0)
canAsk = true;
synchronized(resource) {
//锁住可供资源检查是否超标
for(int i = 0; i < type; i++) {
if(request[i] > resource[i])
//如果申请资源超过可供资源则等待一段时间后重新申请
return false;
}
}
return canAsk;
}
private void tryRequest() {
//创建副本尝试分配请求
synchronized(resource) {
for(int i = 0; i < type; i++)
//依然要防止请求量超出范围
if(request[i] > resource[i])
return;
for(int i = 0; i < type; i++) {
//复制资源量并减去需求量到一个副本上
copyResource[i] = resource[i];
copyResource[i] -= request[i];
}
System.out.print(getName() + " ask for:");
for(int i = 0; i < type; i++)
System.out.print(" " + request[i]);
System.out.println("");
if(checkSafe() == true) {
//如果检查安全则将副本值赋给资源量并修改占有量和需求量
for(int i = 0; i < type; i++) {
resource[i] = copyResource[i];
allocation[i] += request[i];
need[i] -= request[i];
}
System.out.println(getName() + " request succeed!");
}
else
System.out.println(getName() + " request fail!");
}
}
private boolean checkSafe() {
//银行家算法检查安全性
synchronized(bank) {
//将线程资源信息放入二维资源分配表检查安全性,0~type可用资源/type~type*2所需资源/type*2~type*3占有资源/type*3~-1可用+占用资源
for(int i = 0; i < bank.length; i++) {
for(int j = type; j < type*2; j++) {
table[i][j] = bank[i].need[j%type];
}
for(int j = type*2; j < type*3; j++) {
table[i][j] = bank[i].allocation[j%type];
}
}
//冒泡排序按需求资源从小到大排
for(int i = 0; i < bank.length; i++) {
for(int j = i; j < bank.length-1; j++) {
sort(j, 4);
}
}
//进行此时刻的安全性检查
for(int i = 0; i < type; i++) {
table[0][i] = copyResource[i];
table[0][i+type*3] = table[0][i] + table[0][i+type*2];
if(table[0][i+type*3] < table[1][i+type])
return false;
}
for(int j = 1; j < bank.length-1; j++) {
for(int k = 0; k < type; k++) {
table[j][k] = table[j-1][k+type*3];
table[j][k+type*3] = table[j][k] + table[j][k+type*2];
if(table[j][k+type*3] < table[j+1][k+type])
return false;
}
}
}
return true;
}
private void sort(int j, int k) {
//递归冒泡排序
int tempNum;
if(table[j][k] > table[j+1][k]) {
for(int i = type; i < type*2; i++) {
tempNum = table[j][i];
table[j][i] = table[j+1][i];
table[j+1][i] = tempNum;
}
/*temp = bank[j];
bank[j] = bank[j+1];
bank[j+1] = temp;*/
}
else if(table[j][k] == table[j+1][k] && k < type*2) //此资源量相同时递归下一个资源量排序并且防止超出范围
sort(j, k+1);
}
private boolean noNeed() {
//是否还需要资源
boolean finish = true;
for(int i = 0; i < type; i++) {
if(need[i] != 0) {
finish = false;
break;
}
}
return finish;
}
}
public static void main(String[] args) {
ThreadTest t = new ThreadTest();
//后台线程,设定程序运行多长时间后自动结束
new Timeout(30000, "---Stop!!!---");
}
}
3. 如何在java中调用非java语言编写的代码??
是可以嵌入的。
一般说来都是利用DLL文件的形式去调用的。你首先在JAVA里面写上一段代码。把函数的实现给空出来,然后用C或C++去实现你空出来的那个函数的。然后将C或C++给编译成DLL文件的。然后将DLL文件放到你的工程目录下面,并在JAVA代码中使用LOADLIBRARY()将你写的这个DLL文件包含进去。
基本上就可以实现了。
但是说起来简单但是做起来还是比较复杂的e 。多查查书和网站上的东西吧。。
4. Java语言编程,求代码!!
1.
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
int a[]= new int[6];
for(int i=0;i<a.length;i++){
int m=scanner.nextInt();
a[i]=m;
}
int temp;
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length-i-1;j++){
if(a[i]>a[i+1]){
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
for(int i=0;i<a.length;i++){
}
System.out.println("最大值是:"+a[a.length-1]+"\t"+"最小值是:"+a[0]);
}
}
2.
package mypackage;
import java.util.Scanner;
public class Student {
public int id;
public String name;
public String sex;
public void init(){
this.id=23;
this.name="wo";
this.sex="女";
}
public void display(){
System.out.println(id);
System.out.println(name);
System.out.println(sex);
}
public String modified(String name){
this.name=name;
return name;
}
}
test类
package test;
import mypackage.Student;
public class StuTest {
public static void main(String args[]){
Student t=new Student();
t.init();
t.display();
t.modified("ni");
}
}
3.
package test;
import java.util.Scanner;
public class Test {
public static void main(String args[]){
for(;;){
System.out.println("请输入考试成绩等级(A-Z 或者 a-z):如果想要退出请选择0");
Scanner scanner=new Scanner(System.in);
String str=scanner.next();
if(str.equals("0")){
break;
}
while(true){
if(str.length()>1){
System.out.println("输入的长度过长,请重新输入 !!!");
continue;
}
else if(str.length()==1){
char c=str.charAt(0);
int m=c;
if((m>=65 && m=97&&m<=101)){
if(str.equals("A")||str.equals("a")){
System.out.println(str+"所对应的分数段为:"+"90分以上");
break;
}
else if(str.equals("B")||str.equals("b")){
System.out.println(str+"所对应的分数段为:"+"80分以上");
break;
}
else if(str.equals("C")||str.equals("c")){
System.out.println(str+"所对应的分数段为:"+"70分以上");
break;
}
else if(str.equals("D")||str.equals("d")){
System.out.println(str+"所对应的分数段为:"+"60分以上");
break;
}
else if(str.equals("E")||str.equals("e")){
System.out.println(str+"所对应的分数段为:"+"59分以下");
break;
}
}
else {
System.out.println("输入的不在A-Z或者a-z之间");
break;
}
}
}
}
}
}
5. 请用Java语言来编写一段程序,要求达到如下要求!(请高手给我源代码)
import java.util.Scanner;
public class Sort{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个字符:");
String str1 = sc.next();
System.out.println("请输入第二个字符:");
String str2 = sc.next();
char c1 = str1.charAt(0);
char c2 = str2.charAt(0);
int asc1 = (int)c1;
int asc2 = (int)c2;
System.out.println("第一个字符" + str1 + "的ascii码是:" + asc1);
System.out.println("第二个字符" + str2 + "的ascii码是:" + asc2);
if(asc1 > asc2){
System.out.println("大的是:" + str1);
}else{
System.out.println("大的是:" + str2);
}
}
}
6. 求java if语句的调用代码
1.假设有F:\\a.java,和F;\\b.java
2.当输入1的时候,打印出a.java的内容,当输入2的时候出 ,打印出b.java的内容
3.至于读取文件后的操作就随便你了:
public static void main(String[] args) throws IOException {
while (true) {
System.out.println("请输入命令:");
Scanner input = new Scanner(System.in);
int command = input.nextInt();
String str = "";
if (command == 1) {
FileReader file = new FileReader("F:\\a.java");
BufferedReader buffer = new BufferedReader(file);
while((str=buffer.readLine())!=null){
System.out.println(str);
}
continue;
} else if (command == 2) {
FileReader file = new FileReader("F:\\b.java");
BufferedReader buffer = new BufferedReader(file); while((str=buffer.readLine())!=null){
System.out.println(str);
}
continue;
}
}
}
7. 有关Java语句执行时间(想优化代码), 求助高手!
图像处理,是用到了文件处理类吗,那么最好一定要缓存类,这样楼主
BufferedInputStream bis=new BufferedInputStream(FileInpuStream)这样,然后再操作用到缓存类速度可以快很多,楼主试试吧,如果实在还是没有提高把你相关的算法再check一下,看是否有性能问题
8. Java语言代码问题
if else 本身没有问题,但是这种写法不是很好。
少了两个大括号 }}
还有哦,把x<=800改成x<=8000。
import java.util.Scanner;
public class CalcDiscount{
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Scanner input = new Scanner(System.in);
int x = input.nextInt();
System.out.println("请输入会员积分");
if (x < 2000){
System.out.println("该会员享受的折扣是0.9");
}else{ if (x>=2000 && x<=4000){
System.out.println("该会员享受的折扣是0.8");
}else{ if (x>=4000 && x<=8000){
System.out.println("该会员享受的折扣是0.7");
}else{
System.out.println("该会员享受的折扣是0.6");
}
}
}
}
}