标题: 为什么报错? [打印本页] 作者: 郭佳佳 时间: 2012-8-29 08:34 标题: 为什么报错? class Program
{
//第四题:定义两个变量如:a b分别赋值为10和5,写程序交换两个变量的值。
static void Main(string[] args)
{
int a = 10;
int b = 5;
Console.WriteLine("交换前:a={0},b={1}", a, b);//输出交换前的值
int temp;
temp = a;
a = b;
b = temp;//交换前变量的值
Console.WriteLine("交换后:a={0},b={1}", a, b);//输出交换后的值
Console.ReadKey();
分享视频中交换两个变量的值:
using System;
using System.Collection.Gerneric;
using System.Linq;
using System.Text;
namespace Test3
{
class Program
{
static void Main(string[] args)
{
int i1=10;
int i2=20;
Console.WriteLine("i1={0},i2={1}",i1,i2);
int i3;
i3 = i1;
i1 = i2;
i2 = i3;
Console.WriteLine("i1={0},i2={1}",i1,i2);
Console.ReadLine();
}
}
}
static void Main(string[] args)
{
int a = 10;
int b = 5;
Console.WriteLine("交换前:a={0},b={1}", a, b);//输出交换前的值
int temp;
temp = a;
a = b;
b = temp;//交换前变量的值
Console.WriteLine("交换后:a={0},b={1}", a, b);//输出交换后的值
Console.ReadKey();
public class Hello{
public static void main(String[] args) {
int a=2,b=5;//设定初值
yihuo(a,b);
System.out.println("-------------");
temp(a,b);
}
/*
* 不用第三方变量直接交换
*/
public static void yihuo(int a,int b){
a=a^b;//把a与b的异或值赋给a;
b=a^b;//相当于a^b^b;也就是把a值赋值给b;
a=a^b;//相当于a^a^b;也就是把b值赋值给a;
System.out.println("a="+a+",b="+b);
}
/*
* 用第三方变量
*/
public static void temp(int a,int b){
int temp=b;
b=a;
a=temp;
System.out.println("a="+a+",b="+b);
}
}作者: 尹晓乐 时间: 2012-9-2 07:11
int a = 10;
int b = 5;
int c = a; //声明一个中间变量
a = b;
b = c;
Console.WriteLine("a="+a);
Console.WriteLine("b="+b);
Console.ReadKey();作者: 尹晓乐 时间: 2012-9-2 07:19
感觉你们的怎么这么麻烦?
int a = 10;
int b = 5;
Console.WriteLine("交换前:a={0},b={1}", a, b);
int c = a; //声明一个中间变量
a = b;
b = c;
Console.Write("交换后:a="+a);
Console.WriteLine(",b="+ b);
Console.ReadKey();作者: 廖创发 时间: 2012-9-2 11:30
试过了,没有错误,可以正常运行,运算结果也正确。可能是你敲完代码后不小心按错了哪个键作者: 李后量 时间: 2012-9-2 12:17
对啊,没什么问题啊,是不是没有复制过来的代码不小心多输入了个什么啊